We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Interesting how most Java solutions subtract, mine does not (don't think it's better, simply different). Of course it uses more if's. Also, IMHO Math.max() is overkill for just two values.
public static int height(Node root) {
int leftHeight = 0;
int rightHeight = 0;
if (root.left != null) {
leftHeight = 1 + height(root.left);
}
if (root.right != null) {
rightHeight = 1 + height(root.right);
}
return leftHeight > rightHeight ? leftHeight : rightHeight;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Height of a Binary Tree
You are viewing a single comment's thread. Return to all comments →
Interesting how most Java solutions subtract, mine does not (don't think it's better, simply different). Of course it uses more if's. Also, IMHO Math.max() is overkill for just two values.