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.