Python Programming Practice: LeetCode #104 -- Maximum Depth of Binary Tree

preview_player
Показать описание
In this episode of Python Programming Practice: LeetCode #104 -- Maximum Depth of Binary Tree

Link to the problem here:

Python Programming Practice is a series focused on teaching practical coding skills by solving exercises on popular coding websites. Note that the solutions seen here may not be the most efficient possible.

I am not going to provide the full code in the video description for this series, since copy and pasting solutions is not in the spirit of doing coding exercises. It is intended that the video will help you think about problems, approaches and how to structure solutions so that you are able to code up a working solution yourself. .

Рекомендации по теме
Комментарии
Автор

best explanation for beginners on youtube! love how you walk us through the steps

davidguo
Автор

My initial approach was to set pointers for left and right and use a while loop to add plus one until there is no left or right child left, but I can not find a proper way to code that.

marianpascu
Автор

class Node:
def __init__(self, data) -> None:
self.data=data
self.left=self.right=None

depth=0
l=[]

def max_depth(root, l, depth):
if root is None:
return
if root.left is None and root.right is None:
l.append(depth)
return
else:
depth+=1
max_depth(root.left, l, depth)
max_depth(root.right, l, depth)



root=Node(9)
root.left=Node(10)
root.right=Node(7)
root.right.left=Node(1)
root.right.right=Node(2)






max_depth(root, l, depth)
print(max(l))

dipesh