题目
思路
对于任意节点
- 先翻转左子树
- 再翻转右子树
- 交换左右孩子节点
Java
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
invertTree(root.left);
invertTree(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}