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.