a trick for recursive problems

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


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

Still waiting for the trick part in the video

TECHADVICETEAM
Автор

Like we can do it by first of all calculate height of binary tree by taking r=height(root.right) and l=height(root.left) and and return Math.max(l, h)+1 and create basic cases like if root is null then no return for diameter and make 3 variables d1=diameter (root.left) d2=diameter (root.right) and and finally return Math.max(Math.max(d1, d2), d3) and finally we got diameter of BT

ChiragSharma-pbrg
Автор

You can also write a simple traditional bfs first. Then just call that on root.left and root.right separately on their own.

a.m.
Автор

Why update self.res inside dfs and use return value form first dfs call?

CAMOBAP
Автор

This is so much worse in C++ 💀. I should switch to python

king
Автор

Why are we returning max in dfs function = 1 + max (left, right)?

mystikyogi