leetcode-226 翻转二叉树

题目描述:翻转一棵二叉树。

代码实现

递归实现

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null || (root.left == null && root.right == null)){
return root;
}
TreeNode node = invertTree(root.left);
root.left = invertTree(root.right);
root.right = node;
return root;
}
}