answersLogoWhite

0

Really the best way to traverse any binary tree is recursion. In this case we are going to be some node defined as having 3 values, a pointer to a left node, a pointer to a right node, and a value.

then in psudocode we can do it as:

int height(node n, int depth){

int leftDepth;

int rightDepth;

if(n.left != NULL)

leftDepth = height(n.left, depth+1)

else

leftDepth = depth;

if(n.right != NULL)

rightDepth = height(n.right, depth+1)

else

rightDepth = depth;

if(leftDepth > rightDepth) return leftDepth;

return rightDepth;

}

Essentially what you are doing is calling the algorithm on both the left and right nodes which in turn will call it on their left and right nodes, down to where all the nodes are null. Then what is returned is the greater depth of the two; because it will traverse before returning a depth, and only traverses if there is a deeper node, it will return the depth of the deepest node, or the height of the binary tree.

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How do you calculate the height of a binary tree?

To calculate the height of a binary tree, you can use a recursive algorithm that traverses the tree and keeps track of the height at each level. The height of a binary tree is the maximum depth of the tree, which is the longest path from the root to a leaf node.


How to find the height of a binary tree?

To find the height of a binary tree, you can use a recursive algorithm that calculates the height of the left and right subtrees, and then returns the maximum height plus one. This process continues until the height of the entire tree is calculated.


How to calculate the height of a binary tree?

To calculate the height of a binary tree, you can use a recursive algorithm that finds the maximum height of the left and right subtrees, and then adds 1 to the maximum height. This process is repeated for each node in the tree until the height of the entire tree is calculated.


What is the method to find the height of a binary search tree in Java?

To find the height of a binary search tree in Java, you can use a recursive method that calculates the height of the left and right subtrees and returns the maximum height. This can be implemented by defining a method that takes the root node of the tree as input and recursively calculates the height of the tree.


What is the maximum height of a binary tree with a given number of nodes?

The maximum height of a binary tree with 'n' nodes is 'n-1'.


Algorithm that prompts a user to entre his or her height and store the user input in a variable height?

int height; print("Enter height"); height=getString();


What is the formula to calculate the height of a binary tree?

The formula to calculate the height of a binary tree is h log2(n1) - 1, where h is the height of the tree and n is the number of nodes in the tree.


What strategies can be employed to solve the box stacking problem efficiently?

To solve the box stacking problem efficiently, strategies such as dynamic programming, sorting boxes based on dimensions, and using a recursive algorithm can be employed. These methods help in finding the optimal arrangement of boxes to maximize the total height of the stack.


Design an algorithm that prompts the user to enter his or her height and stores the user's input in a variable name height?

int height; print("Enter height"); height=getString();


Height of a binary heap?

log2(N+1)


What is the binary tree height formula and how is it calculated?

The height of a binary tree is calculated using the formula: height max(height(left subtree), height(right subtree)) 1. This formula determines the maximum number of edges from the root to the farthest leaf node in the tree.


The height of Complete Binary tree is in terms of :option1. n2. logn3. n^2?

The height of a complete binary tree is in terms of log(n) where n is the number of nodes in the tree. The height of a complete binary tree is the maximum number of edges from the root to a leaf, and in a complete binary tree, the number of leaf nodes is equal to the number of internal nodes plus 1. Since the number of leaf nodes in a complete binary tree is equal to 2^h where h is the height of the tree, we can use log2 to find the height of a complete binary tree in terms of the number of nodes.