Count Good Nodes in Binary Tree - Leetcode 1448 - Trees (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Thanks for the great explanation! Just to add on, this question can also be done by using any of the 3 traversal techniques, preorder, postorder or inorder and keeping a track of the max at each node.
def dfs(root, maxval):
if not root:
return 0

left = dfs(root.left, max(maxval, root.val))
right = dfs(root.right, max(maxval, root.val))
res = left+right
if root.val >= maxval:
res+=1
return res
return dfs(root, root.val)

siddhantkumar
Автор

please keep solving leetcode questions.. your explanations are great

chuckyb-ddc
Автор

It would have been better if you had also done a dry run on that code.

mahigns
Автор

What is the reason you solve it iteratively instead recursively? Can you explain it?

gingerjiang
visit shbcf.ru