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.
Calculates the height of a binary tree recursively.
`
public static int recHeight(Node node, int counter) {
// Base Case: If the node is null, return the current counter as the height
if (node == null) {
return counter;
}
// Recursive Case: Calculate the height of the left and right subtrees
int leftHeight = recHeight(node.left, counter + 1);
int rightHeight = recHeight(node.right, counter + 1);
// Return the greater of the two heights
return Math.max(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 →
Calculates the height of a binary tree recursively. ` public static int recHeight(Node node, int counter) { // Base Case: If the node is null, return the current counter as the height if (node == null) { return counter; }
} `