Saved Bookmarks
| 1. |
Write a recursive function to calculate the height of a binary tree in Java. |
Answer»
int data; Node left; Node right; }
{ if (node == null) return 0; // If node is null then height is 0 for that node. else { // compute the height of each subtree int leftHeight = heightOfBinaryTree(node.left); int rightHeight = heightOfBinaryTree(node.right); //use the larger among the left and right height and plus 1 (for the root) return Math.max(leftHeight, rightHeight) + 1; } } |
|