Tree
Traversals, BSTs, construction, path प्रॉब्लेम्स आणि स्पष्ट recursive contracts सह tree DP.
DSAमध्यम24 तास8 पॅटर्न्स · 40 प्रॉब्लेम्स
0%
तुमची प्रगतीप्रॉब्लेम आणि केस स्टडीची नावे, कोड आणि तांत्रिक कीवर्ड्स इंग्रजीतच ठेवले आहेत.
मूळ कल्पना
Recursive contract ठरवा: एक call काय परत करतो, आणि कोणते global उत्तर अपडेट करू शकतो. Bottom-up aggregates साठी post-order, context खाली पाठवण्यासाठी pre-order, levels साठी BFS.
केव्हा वापरावे
- Hierarchical डेटा, BSTs, nodes मधील paths
- Level-निहाय प्रश्न (BFS)
- Subtrees वरील aggregates (height, diameter, sums)
टेम्पलेट
def diameter(root):
best = 0
def height(node):
nonlocal best
if not node:
return 0
l, r = height(node.left), height(node.right)
best = max(best, l + r)
return 1 + max(l, r)
height(root)
return bestComplexity
O(n) time, O(h) recursion stack (skewed trees मध्ये worst case O(n)).
सामान्य चुका
- परत केलेले मूल्य आणि global उत्तर यांचा गोंधळ
- फक्त parent शी तुलना करून BST validation (bounds लागतात)
- Skewed इनपुटवर खोल recursion च्या मर्यादा
या कोर्समधील पॅटर्न्स
ओळखण्याचे संकेत
DFSBFSBSTConstructionTree DPPath/Distance