- Invert Binary Tree-Number226
解题思路:
交换左右子树,将左子树和右子树看作“数”,那么该问题就跟交换两个数类似,只不过需要采用递归的方法来进行交换(非递归也可以)
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
TreeNode temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
return root;
}
}
- Maximum Depth of Binary Tree