find height of binary search tree python
def getHeight(self,root):
return -1 if root is None else 1 + max(self.getHeight(root.left), self.getHeight(root.right))
find height of binary search tree python
def getHeight(self,root):
return -1 if root is None else 1 + max(self.getHeight(root.left), self.getHeight(root.right))
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
# define a Class Tree, to intiate the binary tree
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def height(root):
# Check if the binary tree is empty
if root is None:
# If TRUE return 0
return 0
# Recursively call height of each node
leftAns = height(root.left)
rightAns = height(root.right)
# Return max(leftHeight, rightHeight) at each iteration
return max(leftAns, rightAns) + 1
# Test the algorithm
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
print("Height of the binary tree is: " + str(height(root)))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us