Skip to content

Commit 4190374

Browse files
committed
feat: buildTree 문제풀이 추가
1 parent 72f122b commit 4190374

File tree

1 file changed

+14
-0
lines changed
  • construct-binary-tree-from-preorder-and-inorder-traversal

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 시간복잡도: O(n2)
2+
// 공간복잡도: O(n)
3+
4+
var buildTree = function(preorder, inorder) {
5+
if (!preorder.length || !inorder.length) return null;
6+
7+
const root = new TreeNode(preorder[0]);
8+
const mid = inorder.indexOf(root.val);
9+
10+
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
11+
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
12+
13+
return root;
14+
};

0 commit comments

Comments
 (0)