leetCode之旅(12)-反转二叉树
背景描述
Homebrew 是 OS X 平台上的包管理工具。用其官网的话说就是:
the missing package manager for OS X | OS X 平台遗失的包管理器。
相信在用 Mac 的程序员,差不多都知道 Homebrew。
Homebrew 的开发者是 Max Howell。今天 Max 在推特发帖:
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.
谷歌:虽然我们 90% 工程师都在用你写的软件(Homebrew),但你不能在白板上反转二叉树,所以滚蛋。
这条推在开发者圈内引发热议。有网友认为,谷歌该改改面试流程了。
##
## 所以我把这道题自己做出来了,感觉很有成就感 ##
题目描述:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
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.
思路分析:
相这种算法题目,我的思路是尽量用递归解决,因为二叉树本身就是一个递归的结构,同时注意判断空值判断,以及返回值
代码:
/**
* 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) {
TreeNode tmp = null;
tmp = root.right;
root.right = root.left;
root.left = tmp;
invertTree(root.left);
invertTree(root.right);
return root;
}
return null;
}
}
leetCode之旅(12)-反转二叉树的更多相关文章
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
- leetCode题解之反转二叉树
1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...
- LeetCode OJ:Invert Binary Tree(反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- A1102 | 反转二叉树
#include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...
- 【python】Leetcode每日一题-反转链表 II
[python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
随机推荐
- 带你深入理解STL之迭代器和Traits技法
在开始讲迭代器之前,先列举几个例子,由浅入深的来理解一下为什么要设计迭代器. //对于int类的求和函数 int sum(int *a , int n) { int sum = 0 ; for (in ...
- [ExtJS5学习笔记]第二十三节 Extjs5中表格gridpanel的列格式设置
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39665979 官方文档:http://docs.sencha.com/extjs/5. ...
- MySQL聚簇索引的使用介绍
MySQL聚簇索引保证关键字的值相近的元组存储的物理位置也相同(所以字符串类型不宜建立聚簇索引,特别是随机字符串,会使得系统进行大量的移动操作),且一个表只能有一个聚簇索引.因为由存储引擎实现索引,所 ...
- 06_NoSQL数据库之Redis数据库:Redis的高级应用之登录授权和主从复制
Redis高级实用特征 安全性(登录授权和登录后使用auth授权) 设置客户端连接后进行任何其他指定前需要使用的密码. 警告:因为redis速度相当快,所以在一台比较好的服务器下,一个外部的用户 ...
- Hessian探究(一)Hessian入门示例
一.hessian的maven信息: [html] view plain copy print? <dependency> <groupId>com.caucho</gr ...
- 【Unity Shaders】Lighting Models 介绍
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- Android学习之Animation(三)
今天观看了一个关于android动画的一些知识,就顺便记录下来,以备之后的学习和参考. 在XML文件中使用LayoutAnimationController 第一步: 在res/anim文件夹下创建一 ...
- 软考之路--从生活着手,看PV如何操作
PV操作,是软考当中一个很重要的考点,一听到这个名词,顿时赶脚高大上有么有,在软考的历年试题中,也不乏PV操作的身影,老师也对PV操作进行了一次讲课,那时年少,听得稀里糊涂,也不是很理解,在小编的理解 ...
- java的map
Map是一种把键对象和值对象进行关联的容器,而一个值对象又可以是一个Map,依次类推,这样就可形成一个多级映射. 这里有详细的论述http://www.oracle.com/technetwork/c ...
- Using mysqldump for Backups(备份还原数据库实例及参数详细说明)
官网mysqldump命令如下: Using mysqldump for Backups https://dev.mysql.com/doc/refman/5.7/en/using-mysqldump ...