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.