Skip to content

Commit 6ac948d

Browse files
committed
invert-binary-tree solution
1 parent 4cf7b7f commit 6ac948d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

invert-binary-tree/moonjonghoo.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var invertTree = function (root) {
2+
if (root === null) return null;
3+
4+
let queue = [root];
5+
6+
while (queue.length > 0) {
7+
let node = queue.shift();
8+
9+
// 왼쪽과 오른쪽 자식을 교환
10+
[node.left, node.right] = [node.right, node.left];
11+
12+
if (node.left) queue.push(node.left);
13+
if (node.right) queue.push(node.right);
14+
}
15+
16+
return root;
17+
};

0 commit comments

Comments
 (0)