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

मैंने 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.