Leetcode 226. Invert Binary Tree
2017-12-14 本文已影响4人
ShutLove
Invert a binary tree.
4
/
2 7
/ \ /
1 3 6 9
to
4
/
7 2
/ \ /
9 6 3 1
思路:
递归的翻转每个节点的左右子树,然后交换每个节点的左右子树。
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}