Horje
Height of Binary tree Java Code Example
height of binary tree java
public static int height(Node root) 
{
  if(root==null)
  	return -1;
  if(root.left==null && root.right ==null)
  	return 0;
  return  1 + Math.max(height(root.left),height(root.right));
}


// node Structure for reference
    public class Node
    {
        public int data;
        public Node left;
        public Node right;
        public Node(int data)
        {
            this.data = data;
        }
    }
height of a binary tree
int height(Node* root)
{
    // Base case: empty tree has height 0
    if (root == nullptr)
        return 0;
 
    // recur for left and right subtree and consider maximum depth
    return 1 + max(height(root->left), height(root->right));
}
find height of a tree
// finding height of a binary tree in c++.
int maxDepth(node* node)  
{  
    if (node == NULL)  
        return 0;  
    else
    {  
        /* compute the depth of each subtree */
        int lDepth = maxDepth(node->left);  
        int rDepth = maxDepth(node->right);  
      
        /* use the larger one */
        if (lDepth > rDepth)  
            return(lDepth + 1);  
        else return(rDepth + 1);  
    }  
}  
Height of Binary tree Java
public static int height(Node root) 
{
  if(root==null)
  	return -1;
  if(root.left==null && root.right ==null)
  	return 0;
  return  1 + Math.max(height(root.left),height(root.right));
}


// node Structure for reference
    public class Node
    {
        public int data;
        public Node leftChild;
        public Node rightChild;
        public Node(int data)
        {
            this.data = data;
        }
    }
Height Of Binary Tree


height(10) = max(height(5), height(30)) + 1

height(30) = max(height(28), height(42)) + 1
height(42) = 0 (no children)
height(28) = 0 (no children)

height(5) =  max(height(4), height(8)) + 1
height(4) = 0 (no children)
height(8) = 0 (no children)






Java

Related
getarguments().getstring updates android Code Example getarguments().getstring updates android Code Example
java timeout Code Example java timeout Code Example
how to add classpath in spring boot Code Example how to add classpath in spring boot Code Example
java find view by id Code Example java find view by id Code Example
array index out of bound exception in java Code Example array index out of bound exception in java Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7