LeetCode #104 Height of Binary Tree(Max Depth) - Python Interview Question(Recursion)

preview_player
Показать описание
Height of Binary Tree(Max Depth)
#Facebook #GoldmanSach #Microsoft

-~-~~-~~~-~~-~-
Please watch: "LRU Cache (With Python Code) "
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

Very nicely explained. I could understand it very well. Thank you for such a high quality video.

shashikantdivekar
Автор

Hi, working with Python3. Some notation: you cannot compare None to something with == sign, need to use "is" operator; then, to use the function in recursion, you need to use "self" annotation, or make the method static.
and, to run this, you need to make some tricks using Pycharm, as you did, simple "run" will not work.

necronlord
Автор

This video reminded me of my friend Nik. Great video!

vanlang
Автор

I got error that error is Node() takes no arguments... how to solve this err

DHAMODHARANK
Автор

1:51 your assumption for height calculation is not correct. Actually you are calculating level of binary tree not height. Height equals to max depth from root node. Or height equals to maximum count of edges from root to leaves node. Here, height should be 3 not 4.

AshisRaj
Автор

I am new to programming, what is the use of such programs in real life scenarios?

learnvik
Автор

mam why you have used key in the constructor .

missionkillgaming
Автор

videos are very good, would have been best without that annoying super loud intro music ;)

PriyeshPrateek
Автор

Nice code.... use below suplement code to see the tree traversal :

BInary tree representation and find the min & max path.
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def print_tree_traverse(node, direction, depth ):
# first call of this recursive function shohld be :
# print_tree_inorder(root, 'r')

if node is not None:
# Traverse the left subtree
#direction = direction + '->'+ direction
print(" ({}, {}, depth = {}) ".format(node.key, direction, depth))
# Traverse the right subtree
print_tree_traverse(node.left, direction+'-> left', depth+1)
# Print the key of the current node
#print(node.key)
# Traverse the right subtree
print_tree_traverse(node.right, direction+'-> right', depth+1)
else: depth += 1

def rec_min_hight_binary(root):

if(root == None):
return 0
else:
ldepth =
rdepth =

if(ldepth > rdepth):
return 1+rdepth
else:
return 1+ldepth

def rec_max_hight_binary(root):

if(root == None):
return 0
else:
ldepth =
rdepth =

if(ldepth < rdepth):
return 1+rdepth
else:
return 1+ldepth

# b-tree initialization & feed
depth = 0
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.left.right = Node(7)
root.right.right = Node(6)
print_tree_traverse(root, 'r', 0)

#rec_min_hight_binary(root)
print("Min Depth: ", rec_min_hight_binary(root))
print("Max Depth: ", rec_max_hight_binary(root))

OUTPUT
(1, r, depth = 0)
(2, r-> left, depth = 1)
(4, r-> left-> left, depth = 2)
(7, r-> left-> left-> right, depth = 3)
(5, r-> left-> right, depth = 2)
(3, r-> right, depth = 1)
(6, r-> right-> right, depth = 2)
Min Depth: 2
Max Depth: 3

hydtimepass