226. Invert Binary Tree 翻转二叉树
[抄题]:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要分为r.r = l.l, r.l = l.r来讨论,但是其实这样只能最后判断相等。翻转是每一步都要进行的动作,应该尽早开始
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
ab交换在二叉树中的对应情况就是翻转
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
bfs, stack
[Follow Up]:
[LC给出的题目变变变]:
对称二叉树 · symmetric binary tree
不知道有什么数学规律的时候,分情况讨论:分左右两边
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
return root;
}
}
226. Invert Binary Tree 翻转二叉树的更多相关文章
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LeetCode] 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 wa ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- 【easy】226. Invert Binary Tree 反转二叉树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
随机推荐
- 嵌入式视频采集编程思路(Video 4 Linux)-转
转自:http://zyg0227.blog.51cto.com/1043164/271954 1. linux 内核有video for linux简称V4L.V4L是Linux影像系统与嵌入式影 ...
- (二)Fiddler抓取Firefox、Chrome浏览器上的https协议
Fiddler抓取Firefox.Chrome浏览器上的https协议 安装Fiddler后默认只抓取http协议,如果是https协议的话,浏览器就会提示"您的链接并不安全". ...
- xshell 提示 继续使用此程序必须应用到最新的更新或使用新版本 的解决方案
当打开正在使用的xshell后,提示“继续使用此程序必须应用到最新的更新或使用新版本 ” 是因为我们正在使用的是xshell5 版本,需要我们再安装一个xshell6 版本 我个人使用的是家庭/教育 ...
- MOSS 2013研究系列---修改默认Logo
开发SharePoint2013 的时候,系统里面有一个“SharePoint” 的logo,客户很少不满意,我们的系统不能出现产品的名称,如下图: 咋么修改呢,咨询了广大网友,给出了一个解决方案: ...
- Fluent NHibernate AutoMapping Conventions
前言 使用nhibernate在项目中不管是代码或者xml文件映射方式,如果项目较大编写映射也应该算一笔大的工作量了,使用Fluent Nhibernate自己编写映射规则,将从conventions ...
- linux下mysql提示"mysql deamon failed to start"错误的解决方法
操作系统为centos,网站突然连接不上数据库,于是朋友直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon fai ...
- centos7 yum 安装mysql
介绍在CentOS7上yum安装数据库服务器MySQL Community Server 5.7的方法. 准备 CentOS7默认安装了和MySQL有兼容性的MariaDB数据库,在我们安装MySQL ...
- Tool:Visual Studio
ylbtech-Tool:Visual Studio Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.VS是一个基本完整的开发工具集,它包括了整个软件生命 ...
- 杂项:NoSQL
ylbtech-杂项:NoSQL NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站 ...
- .NET单点登录实现方法----两种
第一种模式:同一顶级域名下cookie共享,代码如下 HttpCookie cookies = new HttpCookie("Token"); cookies.Expires = ...