Skip to content

Commit 01ae490

Browse files
committed
solve: invert binary tree
1 parent 3ac0528 commit 01ae490

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

invert-binary-tree/wogha95.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다.
3+
*
4+
* TC: O(N)
5+
* 모든 트리를 순회합니다.
6+
*
7+
* SC: O(N)
8+
* 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다.
9+
*
10+
* N: tree의 모든 node 수
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* }
20+
*/
21+
/**
22+
* @param {TreeNode} root
23+
* @return {TreeNode}
24+
*/
25+
var invertTree = function (root) {
26+
if (!root) {
27+
return root;
28+
}
29+
[root.left, root.right] = [root.right, root.left];
30+
invertTree(root.left);
31+
invertTree(root.right);
32+
return root;
33+
};

0 commit comments

Comments
 (0)