⬅ Previous Topic
Reverse Level Order Traversal of a Binary Tree using RecursionNext Topic ⮕
Find Diameter of a Binary Tree⬅ Previous Topic
Reverse Level Order Traversal of a Binary Tree using RecursionNext Topic ⮕
Find Diameter of a Binary Tree0
.1 + max(height(left), height(right))
.class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def findHeight(root):
if not root:
return 0
return 1 + max(findHeight(root.left), findHeight(root.right))
if __name__ == '__main__':
# Construct binary tree:
# 1
# / \
# 2 3
# / \
# 4 5
root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3))
print("Height of the tree:", findHeight(root))
⬅ Previous Topic
Reverse Level Order Traversal of a Binary Tree using RecursionNext Topic ⮕
Find Diameter of a Binary TreeYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.