题目
[题目地址](leetcode-cn.com/problems/in…)
翻转一棵二叉树。
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
复制代码
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
解题思路
- 我们用es6结构方式
- 把根节点的左右子树互换
- 再递归把左、右子树翻转
代码如下
var invertTree = function(root) {
if(!root) return null;
//根节点左右子树互换
[root.left,root.right] = [root.right,root.left];
invertTree(root.left)
invertTree(root.right)
return root;
};
至此我们就完成了_LeetCode_226. 翻转二叉树