FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Update invert-binary-tree.js · JnuxTeemo/JavaScript-Algorithms@75af833 · GitHub

Commit 75af833

Browse files
committed
Update invert-binary-tree.js
1 parent f513bbf commit 75af833

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎leetcode/invert-binary-tree.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1+
//反转二叉树
2+
/**
3+
* Question:
4+
* Invert a binary tree.
15
6+
4
7+
/ \
8+
2 7
9+
/ \ / \
10+
1 3 6 9
11+
12+
* to
13+
14+
4
15+
/ \
16+
7 2
17+
/ \ / \
18+
9 6 3 1
19+
20+
*
21+
* /
22+
23+
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* function TreeNode(val) {
28+
* this.val = val;
29+
* this.left = this.right = null;
30+
* }
31+
*/
32+
/**
33+
* @param {TreeNode} root
34+
* @return {TreeNode}
35+
*/
36+
var invertTree = function(root) {
37+
if (root){
38+
// 遍历节点做交换位置
39+
root.left = [invertTree(root.right), root.right = invertTree(root.left)][0];
40+
}
41+
return root;
42+
};

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL