leetcode|Invert Binary Tree

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1
题目:简单明了,反转二叉树。还有一个小插曲,
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
意思是:Homebrew的作者去谷歌面试,被问及白板手写反转二叉树,他。。。。,而后在推特上搞了个大新闻,谷歌搞软件的
哪一个没用过我的homebrew,我没写出来你们就把我挂了批判一番,你们啊,navie。。。膜完上代码,+1s:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
  public TreeNode invertTree(TreeNode root) {
    if(root == null){//递归出口
      return null;
    }

    root.left = invertTree(root.left);//反转左边
    root.right = invertTree(root.right);//反转后边

    //左右互转
    TreeNode temp = root.left;
    root.left = root.right;
    root.right = temp;
    return root;
  }
}






posted @ 2016-07-14 13:54  Roger's  阅读(146)  评论(0编辑  收藏  举报