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;
      }
    }






  • 相关阅读:
    php hook example
    sql 建立数据库,表格,索引,主键
    js 高级函数 之示例
    js 事件
    DOM 操作内容 innerText/innerHTML
    DOM 之 document 查找元素方法
    BOM 之 screen history
    Hadoop源码篇--Client源码
    Spark算子篇 --Spark算子之combineByKey详解
    Pandas库的使用--Series
  • 原文地址:https://www.cnblogs.com/wujunjie/p/5670205.html
Copyright © 2011-2022 走看看