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 ...
随机推荐
- Grunt中批量无损压缩图片插件--grunt-sprite
这是什么 这是一个帮助前端开发工程师将css代码中的切片合并成雪碧图的工具,它的主要功能是: 使用二叉树排列算法,对css文件进行处理,收集切片序列,生成雪碧图 在原css代码中为切片添加backgr ...
- VS2013 类向导 "异常来自 HRESULT:0x8CE0000B" 解决方法
转自 http://blog.csdn.net/skyloveyue/article/details/52105912 我用使用了第二种方法: 改变项目的位置 将项目从原来D盘的位置(D:\proje ...
- IOS开发数据存储篇—IOS中的几种数据存储方式
IOS开发数据存储篇—IOS中的几种数据存储方式 发表于2016/4/5 21:02:09 421人阅读 分类: 数据存储 在项目开发当中,我们经常会对一些数据进行本地缓存处理.离线缓存的数据一般都 ...
- 在WinCE上播放声音、设置透明图片、系统音量 C#
记录一下用到的C#在WinCE平台上的相关技巧备查 1.C#在WinCE上实现透明图片 1 2 3 4 5 6 7 8 9 using System.Drawing.Imaging; pu ...
- 前端基础-CSS的属性相关设置
一 字体属性 二 文本属性 三 背景属性 四 盒子模型 五 盒子模型各部分详解 一. 字体属性 1.font-weight:文字粗细(表格中*为重点) 取值 描述 normal 默认值,标准粗细 ...
- python开发的学生管理系统
python开发的学生管理系统(基础版) #定义一个函数,显示可以使用的功能列表给用户 def showInfo(): print("-"*30) print(" 学生管 ...
- Mint-UI 的 DatetimePicker 日期时间插件的安装与使用
简介:Mint-UI是饿了么出品的基于vue的移动端组件库(element-ui是桌面端) 官网:http://mint-ui.github.io/docs/#/zh-cn2 项目环境:vue-cli ...
- DataSet和泛型之间相互转换
取数据的时候,存储过程返回了多个结果集,后台用DataSet去接收这几个结果集,然后接收之后,需要将结果集转换为不同的实体,于是下面的代码便出现了. /// <summary> /// 将 ...
- 类型“Observable<Response>”上不存在属性“map”
出错提示: angular2 Property 'map' does not exist on type 'Observable<Response>' 类型“Observable<R ...
- C语言之一般树
1.一般树 将这种一般的树转化成我们熟悉的单链表形式,这有三层,每一层都可以看成单链表或者多个分散的单链表 数据节点如下: struct tree { int elem; ...