著名的反转二叉树. 我第一次做也骂街.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
if (root.left != null)
invertTree(root.left);
if (root.right != null)
invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}

这个就没啥好说的. 反转二叉树.
时间复杂度: O(n) n是node的总数量. 相当于是每个node都要翻转一下自己的两个node
空间复杂度: O(h) h是树的高度.