Problem:
Invert a binary tree.
Explanation:
将二叉树轴对称翻转。
My Thinking:
前序遍历,将左右孩子交换。
My Solution:
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null)
return null;
TreeNode leftnode=invertTree(root.left);
TreeNode rightnode=invertTree(root.right);
root.left=rightnode;
root.right=leftnode;
return root;
}
}
Optimum Thinking:
Optimum Solution: