(easy)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 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 || (root.left == null) && (root.right == null))
- return root;
- root=invertTreeLR(root,root.left, root.right);
- return root;
- }
- public TreeNode invertTreeLR(TreeNode root,TreeNode left, TreeNode right) { //left,right位临时引用
- if (left == null && right != null) {
- left = right;
- right = null;
- left=invertTreeLR(left,left.left, left.right);
- } else if (left != null && right == null) {
- right = left;
- left = null;
- right=invertTreeLR(right,right.left, right.right);
- } else if (left == null && right == null){
- }
- else {
- TreeNode tmp = null;
- tmp = left;
- left = right;
- right = tmp;
- left=invertTreeLR(left,left.left, left.right);
- right=invertTreeLR(right,right.left, right.right);
- }
- root.left=left;
- root.right=right;
- return root;
- }
- }
运行结果:
(easy)LeetCode 226.Invert Binary Tree的更多相关文章
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- 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 ...
- Leetcode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- Java for 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 wa ...
- Java [Leetcode 226]Invert Binary Tree
题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解题思路: 我只想说递归大法好. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- 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 ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- 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 ...
随机推荐
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
- 搜索关注点--2014年的google关注点
补充: 网站知名网站收录 网站被世界三大知名网站DMOZ,Yahoo和Looksmart收录 众所周知,Google的Pagerank系统对那些门户网络目录如 DMOZ,Yahoo和Looksmart ...
- Hibernate入门学习(二)
本文主要讲如何搭建Hibernate开发环境和简单实例. 一.搭建开发测试环境 1.1 下载Hibernate 从Hibernate官方网站上下载最新的Hibernate ORM,从Hibernate ...
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- 重复ID的记录,只显示其中1条
--重复ID的记录,只显示其中1条 --生成原始表 select * into #tempTable from ( select '1' as id ,'a' as name union all se ...
- [Freescale]E9学习笔记-LTIB安装配置
转自:http://blog.csdn.net/girlkoo/article/details/44535979 LTIB: Linux Target Image Builder Freescale提 ...
- Android:单元测试Junit的配置
在实际开发中,开发android软件的过程需要不断地进行测试.而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性.... ...
- 黄聪:wordpress中PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312)(转)
近日在升级wordpress 3.2.1和若干插件的过程中,发现了一个wordpress的错误:Allowed memory size of XXX bytes exhausted Fatal err ...
- 关于Cannot assign to 'self' outside of a method in the init family解决方法
有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Can ...
- ADF_Advanced ADF系列2_Fusion应用的客制和个性化(Part2)
2015-02-17 Created By BaoXinjian