Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Root from preorder; split sizes from inorder.
ನಾನು Construct Binary Tree from Preorder and Inorder Traversal (LeetCode 105) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Construction / Serialization ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Pre-order's first value is the root. Find it in the in-order list: everything to its left is the left subtree and everything to the right is the right subtree. The sizes tell you how to split the pre-order list too. Recurse.
Approach
- 1
Map each in-order value to its index.
- 2
build(pre range, in range): root = preorder[pre_start].
- 3
nums_on_left = rootIndex − in_start.
- 4
Recurse for the left and right ranges.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ
Recursive split with index mapPythonO(n) time · O(n) 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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
#preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
#preorder - root,left,right
#inorder - left,root,right
in_map = {}
for i,num in enumerate(inorder):
in_map[num]=i
m = len(preorder)
n = len(inorder)
def build(pre_start,pre_end,in_start,in_end):
if pre_start>pre_end:
return None
if in_start>in_end:
return None
root = TreeNode(preorder[pre_start])
root_in_map_index = in_map.get(preorder[pre_start])
nums_on_left = root_in_map_index-in_start
#left part
#prestart will start from root's next position, since it's left part, it will go till prestart+numsleft
#inend will go till root's last position -1
#right part
#right part will start from prestart+nums_on_left+1, prend, instart will start root's next position to inend
root.left = build(pre_start+1,pre_start+nums_on_left,in_start,root_in_map_index-1)
root.right = build(pre_start+nums_on_left+1,pre_end,root_in_map_index+1,in_end)
return root
return build(0,m-1,0,n-1)
Construct Binary Tree from Preorder and Inorder Traversal — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Construct Binary Tree from Preorder and Inorder Traversal ಹಿಂದಿನ intuition ಏನು?
Pre-order's first value is the root. Find it in the in-order list: everything to its left is the left subtree and everything to the right is the right subtree. The sizes tell you how to split the pre-order list too. Recurse.
Construct Binary Tree from Preorder and Inorder Traversal (LeetCode 105) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Map each in-order value to its index. 2. build(pre range, in range): root = preorder[pre_start]. 3. nums_on_left = rootIndex − in_start. 4. Recurse for the left and right ranges.
Construct Binary Tree from Preorder and Inorder Traversal ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(n). Space: O(n). (Accepted approach: Recursive split with index map.)
Construct Binary Tree from Preorder and Inorder Traversal ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Values must be unique for the index map to work.