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)