Skip to content

Commit a80bec3

Browse files
committed
Invert Binary Tree Solution
1 parent a0be0b8 commit a80bec3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* @param {TreeNode} root
11+
* @return {TreeNode}
12+
*/
13+
var invertTree = function (root) {
14+
if (!root) return null;
15+
16+
const queue = [root];
17+
18+
while (queue.length > 0) {
19+
const current = queue.shift();
20+
21+
// ์ž์‹ ๋…ธ๋“œ๋“ค ๋ฐ”๊พธ๊ธฐ
22+
[current.left, current.right] = [current.right, current.left];
23+
24+
// ์ž์‹ ๋…ธ๋“œ๋“ค์„ ํ์— ์ถ”๊ฐ€
25+
if (current.left) queue.push(current.left);
26+
if (current.right) queue.push(current.right);
27+
}
28+
29+
return root;
30+
};

0 commit comments

Comments
ย (0)