Skip to content

Commit e9b47eb

Browse files
authored
Create code.js
1 parent 2f2349d commit e9b47eb

File tree

1 file changed

+29
-0
lines changed
  • 공예영/2주차/0226-invert-binary-tree

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
let left, right;
18+
19+
while(queue.length > 0){
20+
const current = queue.pop();
21+
left = current.left;
22+
right = current.right;
23+
current.left = right;
24+
current.right = left;
25+
if(left) queue.push(left);
26+
if(right) queue.push(right);
27+
}
28+
return root;
29+
};

0 commit comments

Comments
 (0)