| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f513bbf commit 75af833
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1 +1,42 @@ | |||
| 1 | + //反转二叉树 | ||
| 2 | + /** | ||
| 3 | + * Question: | ||
| 4 | + * Invert a binary tree. | ||
| 1 | 5 | ||
| 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 | + }; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments