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.