描述
翻转一棵二叉树。
示例 1:
输入: 4
/ \
2 7
/ \ / \
1 3 6 9输出: 4
/ \
7 2
/ \ / \
9 6 3 1
题解
这道题的具体解法,可参看 labuladong的这篇文章
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if (!root) return root
let temp = root.left
root.left = root.right
root.right = temp
invertTree(root.left)
invertTree(root.right)
return root
};