175. Invert Binary Tree【LintCode by java】
Description
Invert a binary tree.
Example
1 1
/ \ / \
2 3 => 3 2
/ \
4 4
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void invertBinaryTree(TreeNode root) {
// write your code here
if(root == null)
return ;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = right;
root.right = left;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}
非递归法:
public class Solution {
public TreeNode invertTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
if(root!=null) q.offer(root);
while(!q.isEmpty()){
TreeNode curr = q.poll();
TreeNode tmp = curr.right;
curr.right = curr.left;
curr.left = tmp;
if(curr.left!=null) q.offer(curr.left);
if(curr.right!=null) q.offer(curr.right);
}
return root;
}
}
175. Invert Binary Tree【LintCode by java】的更多相关文章
- 155. Minimum Depth of Binary Tree【LintCode by java】
Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- Lintcode 175 Invert Binary Tree
I did it in a recursive way. There is another iterative way to do it. I will come back at it later. ...
- 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】
Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- 501. Find Mode in Binary Search Tree【LeetCode by java】
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...
- 212. Space Replacement【LintCode by java】
Description Write a method to replace all spaces in a string with %20. The string is given in a char ...
随机推荐
- WinFrom开发小案例
C# 开发环境: VisualStudio2015 数据库: SQLserver2008 程序主界面: 注释: lbl标签: 程序中的lbl标签:编号.人数.姓名.性别.请输入要查询的信息,这里他们只 ...
- ARC下IBOutlet用weak还是strong
原文来自这里. 今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了.但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC.于 ...
- C++笔记008:C++对C的扩展——命名空间 namespace基础
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 第一, 命名空间的意义 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中常见的同名冲突. 我认识两位叫“A”的朋友,一 ...
- 利用binlog2sql闪回丢失数据
today,i'll using the open source tool named "binlog2sql" which is release by danfengch ...
- mariadb或者mysql忘记root密码
windows======================net stop mysql #先停止mysql或者在服务管理里面停止 直接打开Windows的命令行(CMD)窗口(以管理员身份运行),输入 ...
- css:url链接去下划线+点击前黑色+点击时灰色+点击后黑色
一般的文章列表 加了样式之后的效果 附上css代码 /*点击前*/ a:link{ color: black; } /*点击后*/ a:visited{ color: black; } /*点击时*/ ...
- ORA-12541:TNS:无监听程序问题
这种情况可能有多种原因,解决办法如下: 方法1.原因:监听日志listener.log过大,超过4. 步骤: a.暂停监听服务 b.删除listener.log,文件位置:E:\app\Adminis ...
- SSM+poi导入和导出
最原始数据 导入成功后 下载数据 下载后的数据显示 数据变成16条 点击导出可选择 导了两次 看数据变化 数据库字段在下面地址给出 首先贴出Dao层 List<User> findAll ...
- Linux awk基础笔记
正则表达式含义与构成:用单个字符串来描述或者匹配一系列符合某个句法规则的字符串,一般是由普通字符与特殊字符组成 awk 选项 '模式或者命令{编辑指令}' 文件名 awk '//{print}' aw ...
- Python系列之入门篇——python2.7.13安装
Python2.7.13 安装 说明 以下所有操作都基于centos6.9 1. Issue zlib zlib-devel是安装setuptools依赖的模块,需要在安装python之前先安装这两个 ...