Answers for "height of bst tree"

C++
1

height of bst cpp

// Find height of a tree, defined by the root node
int tree_height(Node* root) {
    if (root == NULL) 
        return 0;
    else {
        // Find the height of left, right subtrees
        left_height = tree_height(root->left);
        right_height = tree_height(root->right);
          
        // Find max(subtree_height) + 1 to get the height of the tree
        return max(left_height, right_height) + 1;
}
Posted by: Guest on October-10-2021
2

height of the tree

int height(Node* root) {
        // Base Condition : if root is already null. then height must be -1 to make balance with recursion call...
        if(!root) return 0;
        
        // Actual Return statement.. for recursion call..
        return 1 + max(height(root->left), height(root->right));
    }
Posted by: Guest on December-02-2021
1

Height Of Binary Tree

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;
        }
    }
Posted by: Guest on January-18-2022

Browse Popular Code Answers by Language