-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solution: construct-binary-tree-from-preorder-and-inorder-traversal
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
construct-binary-tree-from-preorder-and-inorder-traversal/dusunax.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# 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: | ||
''' | ||
A. ์ฌ๊ท ํ์ด | ||
preorder์ inorder์ ๊ฐ๊ฐ์ ๋ฒ์๋ฅผ ์กฐ์ ํ์ฌ ํธ๋ฆฌ๋ฅผ ์์ฑ | ||
''' | ||
def buildTreeA(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
def setTree(pre_left, pre_right, in_left, in_right): | ||
# ์ฌ๊ท ์ข ๋ฃ ์กฐ๊ฑด: preorder ๋ฒ์๊ฐ ์ ํจํ์ง ์์ ๊ฒฝ์ฐ | ||
if pre_left > pre_right: | ||
return None | ||
|
||
val = preorder[pre_left] # preorder์ ํ์ฌ ๋ฃจํธ ๋ ธ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ | ||
mid = TreeNode(val) # ๋ฃจํธ ๋ ธ๋๋ฅผ ๋จผ์ ์์ฑ | ||
|
||
mid_inorder = inorder_idx_map[val] # ๋ฃจํธ ๋ ธ๋์ inorder ์ธ๋ฑ์ค ๊ฐ์ ธ์ค๊ธฐ | ||
left_size = mid_inorder - in_left # ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ํฌ๊ธฐ ๊ณ์ฐ | ||
|
||
# ์ผ์ชฝ ์๋ธํธ๋ฆฌ ์์ฑ: preorder์ inorder์ ๋ฒ์๋ฅผ ์ผ์ชฝ ์๋ธํธ๋ฆฌ๋ก ์กฐ์ | ||
mid.left = setTree( | ||
pre_left + 1, pre_left + left_size, in_left, mid_inorder - 1 | ||
) | ||
|
||
# ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ์์ฑ: preorder์ inorder์ ๋ฒ์๋ฅผ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ก ์กฐ์ | ||
mid.right = setTree( | ||
pre_left + left_size + 1, pre_right, mid_inorder + 1, in_right | ||
) | ||
|
||
return mid # ํ์ฌ ๋ ธ๋ ๋ฐํ | ||
|
||
# inorder๋ฅผ ๊ฐ -> ์ธ๋ฑ์ค ๋งตํํ ๋์ ๋๋ฆฌ ์์ฑ (O(n)) | ||
inorder_idx_map = {value: idx for idx, value in enumerate(inorder)} | ||
|
||
# ํธ๋ฆฌ ์์ฑ ์์ (preorder์ inorder ์ ์ฒด ๋ฒ์ ์ฌ์ฉ) | ||
return setTree(0, len(preorder) - 1, 0, len(inorder) - 1) | ||
|
||
|
||
''' | ||
# B. ์ฌ๊ท ํ์ด + ๊ณต๊ฐ ์ต์ ํ | ||
# ๋ ํผ๋ฐ์ค ๋งํฌ์ ํ์ด 2: https://www.algodale.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ | ||
# ํน์ง: ์ํ ์๋ง๋ค ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋ ๊ณผ์ ์ด ์์ | ||
''' | ||
def buildTreeB(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
# pre: ํ์ฌ preorder์์ ํ์ธํ ์ธ๋ฑ์ค | ||
# start, end: inorder์์ ์ฌ์ฉํ ์์/์ข ๋ฃ ๋ฒ์ | ||
def setTree(pre, start, end): | ||
# ์ฌ๊ท ์ข ๋ฃ ์กฐ๊ฑด: ๋ฒ์๊ฐ ์๋ชป๋์๊ฑฐ๋ ํธ๋ฆฌ๋ฅผ ๋ ์ด์ ๋ง๋ค ํ์๊ฐ ์๋ ๊ฒฝ์ฐ | ||
if not (pre < len(preorder) and start <= end): # preorder์์ ํ์ธํ ์ธ๋ฑ์ค๊ฐ ๋ฒ์์์ ๋๊ฐ, ํฌ ํฌ์ธํฐ๊ฐ ๋ง๋จ | ||
return None | ||
|
||
val = preorder[pre] # ํ์ฌ ๋ ธ๋์ ๊ฐ | ||
root = inorder.index(val) # ํธ๋ฆฌ/์๋ธํธ๋ฆฌ์ ๋ฃจํธ ๋ ธ๋ ์ธ๋ฑ์ค ์ฐพ๊ธฐ - SC: O(n) | ||
|
||
left = setTree(pre + 1, start, root - 1) | ||
# inorder์์ root๋ ธ๋์ ์ผ์ชฝ์ ์ผ์ชฝ ์๋ธํธ๋ฆฌ | ||
# pre์ ๋ณํ: ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ฃจํธ ๋ ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํด +1 ์ด๋ | ||
|
||
right = setTree(pre + 1 + root - start, root + 1, end) | ||
# inorder์์ root๋ ธ๋์ ์ค๋ฅธ์ชฝ์ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ | ||
# pre์ ๋ณํ: ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ๋ฃจํธ ๋ ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํด +1 ์ด๋ + (root - start) ๐ ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ํฌ๊ธฐ ๋งํผ ๋ ์ด๋ | ||
|
||
return TreeNode(preorder[pre], left, right) # ํธ๋ฆฌ ๋ ธ๋ ์์ฑ | ||
|
||
# preorder ์ต์ด ์ธ๋ฑ์ค = ๋ฃจํธ ๋ ธ๋(0), inorder์ ์ฒ์(0)๊ณผ ๋(len(inorder) - 1) ์ธ๋ฑ์ค | ||
return setTree(0, 0, len(inorder) - 1) | ||
|
||
''' | ||
C. ์ฌ๊ท ํ์ด + ์๊ฐ ์ต์ ํ | ||
๋ ํผ๋ฐ์ค ๋งํฌ์ ํ์ด 3: https://www.algodale.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ | ||
ํน์ง: A์์ preorder๋ฅผ ์ฐพ๋ O(n) ๊ณผ์ ์ ํด์ ํ ์ด๋ธ์ ์ฌ์ฉํ์ฌ O(1)๋ก ์ต์ ํ | ||
''' | ||
def buildTreeC(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
# enumerate: ์ธ๋ฑ์ค์ ๊ฐ์ ๋์์ ๋ฐํ | ||
# inorder๋ฅผ val -> idx๋ก ๋งคํํ ๋์ ๋๋ฆฌ ์์ฑ | ||
inorder_index_map = {val: idx for idx, val in enumerate(inorder)} | ||
# preorder๋ฅผ ์ํํ๊ธฐ ์ํ iterator ๊ฐ์ฒด ์์ฑ | ||
pre_iter = iter(preorder) | ||
|
||
def setTree(start, end): | ||
if start > end: # ์ฌ๊ท ์ข ๋ฃ ์กฐ๊ฑด: ๋ฒ์๊ฐ ์๋ชป๋์๊ฑฐ๋ ํธ๋ฆฌ๋ฅผ ๋ ์ด์ ๋ง๋ค ํ์๊ฐ ์๋ ๊ฒฝ์ฐ | ||
return None | ||
|
||
root_val = next(pre_iter) # ํ์ฌ ๋ ธ๋์ ๊ฐ, ๋งค ์ํ๋ง๋ค ๋ค์ preorder ๋ ธ๋(root)์ ๊ฐ์ ๊ฐ์ ธ์ด | ||
root = inorder_index_map[root_val] # ํธ๋ฆฌ/์๋ธํธ๋ฆฌ์ ๋ฃจํธ ๋ ธ๋ ์ธ๋ฑ์ค๋ฅผ O(1) ์๊ฐ์ผ๋ก ์ฐพ๊ธฐ | ||
|
||
left = setTree(start, root - 1) # ์ผ์ชฝ ์๋ธํธ๋ฆฌ | ||
right = setTree(root + 1, end) # ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ | ||
return TreeNode(root_val, left, right) # ํธ๋ฆฌ ๋ ธ๋ ์์ฑ | ||
|
||
return setTree(0, len(inorder) - 1) # inorder์ ์ฒ์(0)๊ณผ ๋(len(inorder) - 1) ์ธ๋ฑ์ค |