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.

# Tree: Height of a Binary Tree

# Tree: Height of a Binary Tree

coeus + 0 comments For anyone having problem in sample test case. Please note down that they have considerd height of root as '-1'. So your program should return '-1' when root is NULL.

Hlodowig + 0 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.

`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; }`

[deleted] + 0 comments Not python, or scala, or haskell?

blainevanderson + 0 comments There appears to be an error with the code, where the main function is trying to call "getHeight", but the function is named "height". Just thought admins might like to know :)

isarkisov + 0 comments def height(root): if root is None: return -1 return max(1 + height(root.left), 1 + height(root.right))

Load more conversations

Sort 604 Discussions, By:

Please Login in order to post a comment