Tensorum
YouTubeகற்கத் தொடங்குங்கள்

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 best

Complexity

O(n) time, O(h) recursion stack (skewed trees இல் worst case O(n)).

பொதுவான தவறுகள்

  • திருப்பித் தந்த மதிப்பை global விடையுடன் குழப்புவது
  • Parent உடன் மட்டும் ஒப்பிட்டு BST validation (bounds தேவை)
  • Skewed உள்ளீடுகளில் ஆழமான recursion வரம்புகள்

அடையாளக் குறிப்புகள்

DFSBFSBSTConstructionTree DPPath/Distance