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