forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0226-invert-binary-tree.js
46 lines (40 loc) · 1.05 KB
/
0226-invert-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 226. Invert Binary Tree
* https://leetcode.com/problems/invert-binary-tree/
* Difficulty: Easy
*
* Invert a binary tree.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} node
* @return {TreeNode}
*/
var invertTree = function(node) {
if (node) [node.left, node.right] = [invertTree(node.right), invertTree(node.left)];
return node;
};
// var invertTree = function(root) {
// if (root === null) return null;
// const node = new TreeNode(root.value);
// node.right = invertTree(root.left);
// node.left = invertTree(root.right);
// return node;
// };
// var invertTree = function(root) {
// if (root == null) return null;
// const queue = new Queue([root]);
// while (!queue.isEmpty()) {
// let node = queue.pop();
// [node.left, node.right] = [node.right, node.left];
// if (node.left != null) queue.push(node.left);
// if (node.right != null) queue.push(node.right);
// }
// return root;
// }