Skip to content

Commit ed96297

Browse files
authored
[ PS ] : Invert Binary Tree
1 parent 3ca602b commit ed96297

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

invert-binary-tree/uraflower.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
10+
/**
11+
* 이진 트리를 좌우 반전하여 반환하는 함수
12+
* @param {TreeNode} root
13+
* @return {TreeNode}
14+
*/
15+
const invertTree = function (root) {
16+
if (root !== null) {
17+
invertTree(root.right);
18+
invertTree(root.left);
19+
[root.right, root.left] = [root.left, root.right];
20+
}
21+
22+
return root;
23+
};
24+
25+
// 시간복잡도: O(n)
26+
// 공간복잡도: O(n)

0 commit comments

Comments
 (0)