zoukankan      html  css  js  c++  java
  • 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;
      }
    }






  • 相关阅读:
    什么是 DLL?
    如何用vc创建和读取xml文件??
    VC中调用 Excel 的总结
    Excel.cpp和Excel.h
    SQL中也可以用格式字符串定制日期转字符串
    REVERT权限切换
    透明数据加密
    批量恢复数据库
    FILESTREAM
    eclipse Tomcat热启动maven install Jrebel
  • 原文地址:https://www.cnblogs.com/wujunjie/p/5670205.html
Copyright © 2011-2022 走看看