Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 226 · DFS Basics

Invert Binary Tree

LeetCode 226 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Swap children at every node.

எளிதுTreeDepth-First SearchBreadth-First SearchBinary Tree
LeetCode இல் திற ↗

நான் Invert Binary Tree (LeetCode 226) ஐ இப்படித் தீர்த்தேன் — DFS Basics பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Mirroring a tree means swapping every node's left and right children. Do it recursively: invert both subtrees, then swap them.

Approach

  1. 1

    Empty → None.

  2. 2

    Invert left and right subtrees.

  3. 3

    Swap root.left and root.right; return root.

நான் எப்படித் தீர்த்தேன்

Recursive swapPythonO(n) time · O(h) space
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
            if root == None:
                return None
            self.invertTree(root.left)
            self.invertTree(root.right)
            root.left,root.right=root.right,root.left
            return root
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Invert Binary Tree — அடிக்கடி கேட்கப்படும் கேள்விகள்

Invert Binary Tree பின்னாலுள்ள intuition என்ன?

Mirroring a tree means swapping every node's left and right children. Do it recursively: invert both subtrees, then swap them.

Invert Binary Tree (LeetCode 226) ஐ எப்படித் தீர்ப்பது?

1. Empty → None. 2. Invert left and right subtrees. 3. Swap root.left and root.right; return root.

Invert Binary Tree இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(h). (Accepted approach: Recursive swap.)

Invert Binary Tree இல் பொதுவான தவறுகள் என்ன?

Order doesn't matter — pre-order swapping works too.