一. 题目:Invert Binary Tree
invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9to
4 / \ 7 2 / \ / \ 9 6 3 1Trivia:
This problem was inspired by this original tweet by Max Howell :
二. 思路分析
题目大意:给定一颗二叉树,反转二叉树的左右节点。思路比较简单:利用递归的思想,代码如下:
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
final TreeNode left = root.left,
right = root.right;
root.left = invertTree(right);
root.right = invertTree(left);
return root;
}
}