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.