Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 145 · Traversals (Recursive / Iterative)

Binary Tree Postorder Traversal

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

Left → right → node.

எளிதுStackTreeDepth-First SearchBinary Tree

நான் Binary Tree Postorder Traversal (LeetCode 145) ஐ இப்படித் தீர்த்தேன் — Traversals (Recursive / Iterative) பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

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

Intuition

Post-order visits both children before the node itself — the order you'd use to delete a tree or combine child results.

Approach

  1. 1

    dfs(None) returns.

  2. 2

    dfs(left), dfs(right), then record node.val.

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

Recursive DFSPythonO(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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def dfs(node):
            if node==None:
                return None
            dfs(node.left)
            dfs(node.right)
            res.append(node.val)
        dfs(root)
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

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

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

Post-order visits both children before the node itself — the order you'd use to delete a tree or combine child results.

Binary Tree Postorder Traversal (LeetCode 145) ஐ எப்படித் தீர்ப்பது?

1. dfs(None) returns. 2. dfs(left), dfs(right), then record node.val.

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

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

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

Iterative post-order is the trickiest of the three — reversed (node, right, left) is a neat shortcut.