Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 106 · Construction / Serialization

Construct Binary Tree from Inorder and Postorder Traversal

LeetCode 106 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Root = postorder[end]; split by in-order index.

మధ్యస్థంArrayHash TableDivide and ConquerTreeBinary Tree
LeetCode లో తెరవండి ↗

నేను Construct Binary Tree from Inorder and Postorder Traversal (LeetCode 106) ను ఇలా పరిష్కరించాను — Construction / Serialization ప్యాటర్న్‌లోని ఒక మధ్యస్థం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Same idea as pre-order + in-order, but post-order's last value is the root. Its position in the in-order list splits both lists into left and right parts.

Approach

  1. 1

    Index map for in-order values.

  2. 2

    Root from post_end.

  3. 3

    Left subtree uses the first nums_on_left of the post-order range.

  4. 4

    Right subtree uses the rest except the root.

నేను ఎలా పరిష్కరించాను

Recursive split with index mapPythonO(n) time
# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        """
        inorder - left,root,right
        postoder - left,right,root
        inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
        """
        in_map = {}
        m = len(inorder)
        n = len(postorder)
        for i,num in enumerate(inorder):
            in_map[num]=i
        def build(in_start,in_end,post_start,post_end):
            if in_start>in_end:
                return None
            if post_start>post_end:
                return None
            root = TreeNode(postorder[post_end])
            root_in_inmap_index = in_map[postorder[post_end]]
            nums_on_left= root_in_inmap_index-in_start
            root.left=build(in_start, root_in_inmap_index-1,post_start,post_start+nums_on_left-1)
            root.right=build(root_in_inmap_index+1, in_end,post_start+nums_on_left,post_end-1)
            return root
        return build(0,m-1,0,n-1)
        
స్టెప్-త్రూ లోడ్ అవుతోంది…

Construct Binary Tree from Inorder and Postorder Traversal — తరచూ అడిగే ప్రశ్నలు

Construct Binary Tree from Inorder and Postorder Traversal వెనుక ఉన్న intuition ఏమిటి?

Same idea as pre-order + in-order, but post-order's last value is the root. Its position in the in-order list splits both lists into left and right parts.

Construct Binary Tree from Inorder and Postorder Traversal (LeetCode 106) ను ఎలా పరిష్కరించాలి?

1. Index map for in-order values. 2. Root from post_end. 3. Left subtree uses the first nums_on_left of the post-order range. 4. Right subtree uses the rest except the root.

Construct Binary Tree from Inorder and Postorder Traversal యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(n). (Accepted approach: Recursive split with index map.)

Construct Binary Tree from Inorder and Postorder Traversal లో సాధారణ పొరపాట్లు ఏమిటి?

Careful with the post-order ranges — the root sits at the end.