We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9002b76 commit d8759aeCopy full SHA for d8759ae
invert-binary-tree/main.js
@@ -0,0 +1,36 @@
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
+ [4,2,7,1,3,6,9]
11
+
12
+ 4
13
+ / \
14
+ 2 7
15
+ / \ / \
16
+ 1 3 6 9
17
18
19
20
+ * @param {TreeNode} root
21
+ * @return {TreeNode}
22
23
+var invertTree = function(root) {
24
+ if (root === null) return null;
25
26
+ // swap the children
27
+ let tmp = root.left;
28
+ root.left = root.right;
29
+ root.right = tmp;
30
31
+ // do it again until root of the tree
32
+ invertTree(root.left);
33
+ invertTree(root.right);
34
35
+ return root;
36
+};
0 commit comments