Skip to content

Commit 52ad3d1

Browse files
Update taurus09318976.py
1 parent bf2615d commit 52ad3d1

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

construct-binary-tree-from-preorder-and-inorder-traversal/taurus09318976.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- 왼쪽: [9] (인덱스 0까지)
3232
- 오른쪽: [15, 20, 7] (인덱스 2부터)
3333
34-
3단계: 왼쪽 서브트리 크기로 preorder 분할
34+
3단계: 왼쪽 서브트리 크기로 preorder 분할
3535
왼쪽 크기가 1이면 preorder에서도 1개만 가져옴
3636
- 루트 제외: [9, 20, 15, 7]
3737
- 왼쪽 크기(1)만큼: [9]
@@ -69,28 +69,25 @@ def buildTree(self, preorder, inorder):
6969
# 1. 기본 케이스: 빈 배열이면 None 반환
7070
if not preorder or not inorder:
7171
return None
72-
72+
7373
# 2. preorder의 첫 번째가 루트
7474
root_val = preorder[0]
7575
root = TreeNode(root_val)
76-
76+
7777
# 3. inorder에서 루트 위치 찾기
7878
root_index = inorder.index(root_val)
79-
79+
8080
# 4. inorder를 루트 기준으로 분할
8181
left_inorder = inorder[:root_index]
8282
right_inorder = inorder[root_index + 1:]
83-
83+
8484
# 5. preorder도 해당 크기만큼 분할
8585
left_preorder = preorder[1:1 + len(left_inorder)]
8686
right_preorder = preorder[1 + len(left_inorder):]
87-
87+
8888
# 6. 재귀적으로 왼쪽과 오른쪽 서브트리 구성
8989
root.left = self.buildTree(left_preorder, left_inorder)
9090
root.right = self.buildTree(right_preorder, right_inorder)
91-
92-
return root
93-
9491

92+
return root
9593

96-

0 commit comments

Comments
 (0)