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
解题思路:
我只想说递归大法好。
代码如下:
/**
* 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) {
TreeNode temp = null;
if(root == null)
return null;
else{
temp = invertTree(root.left);
root.left = invertTree(root.right);
root.right = temp;
}
return root;
}
}
Java [Leetcode 226]Invert Binary Tree的更多相关文章
- 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 ...
- 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): ...
- (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 ...
- 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(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. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. 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 ...
随机推荐
- stm32之ADC学习
1.stm32中采用的是逐次逼近型模拟数字方式,那么什么是逐次逼近呢? 逐次逼近的方式类似于二分法,以8位数据为例:当输入一个模拟量的时候,首先取这8位数的一半,即1000 0000,与模拟量比较,大 ...
- install ruby and ruby gem
sudo apt-get install ruby #find an folder and: git clone https://github.com/rubygems/rubygems.git cd ...
- NotifyIcon制作任务栏托盘菜单
常用软件飞信.QQ在任务栏中的图标ICO,以及鼠标移动到图标是右键菜单选项 1.首先制作任务栏图标 this.ShowInTaskbar = true; 2.窗体最小化时或者关闭时隐藏到任务栏,有时候 ...
- OnDrawGizmos函数
如果你想绘制可被点选的gizmos,执行这个函数. 这允许你在场景中快速选择重要的物体. 注意: OnDrawGizmos使用相对鼠标坐标 using UnityEngine; using Syste ...
- MyBatis学习笔记之resultMap
使用mybatis不能不说的是resultMap 相比resultClass来说resultMap可以适应更复杂的关系映射,允许指定字段的数据类型,支持“select *” ,并不要求定义 Resul ...
- Linux计算机进程地址空间与内核装载ELF
本文基于Linux™系统对进程创建与加载进行分析,文中实现了Linux库函数fork.exec,剖析内核态执行过程,并进一步展示进程创建过程中进程控制块字段变化信息及ELF文件加载过程. 一.初识Li ...
- Linux操作系统工作的基础
简介: 本文根据 Linux™ 系统工作基础的分析,对存储程序计算机.堆栈(函数调用堆栈)机制和中断机制进行概述.文中将为您提供操作系统(内核)如何工作的细节,进一步从宏观概述结合关键点进行微观(CS ...
- 使用Yeoman搭建 AngularJS 应用 (10) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-unit-tests.html 对于不熟悉的Karma的人来说,这是JavaScript测试框架,这个Angular的生成器包含 ...
- 防止IFRAME页被嵌套
防止IFRAME页被嵌套 //最大化窗口,防止窗口嵌套 if (parent.location != window.location){ parent.location = window.locati ...
- java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z
鸣谢网址:http://stackoverflow.com/questions/25940571/java-lang-nosuchmethoderror-javaxservlet-http-https ...