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 ...
随机推荐
- 使用BSRR和BRR寄存器直接操作STM32的I/O端口
STM32的每个GPIO端口都有两个特别的寄存器,GPIOx_BSRR和GPIOx_BRR寄存器,通过这两个寄存器可以直接对对应的GPIOx端口置'1'或置'0'. GPIOx_BSRR的高16位中每 ...
- Dynamic Ambient Occlusion and Indirect Lighting
This sample was presented on the Nvida witesite, which detail a new idea to calculate the ambient oc ...
- Vue--- 一点车项目 6个小时实际看了10天(完结)
一个项目 环境安装 使用了 cli 脚手架 Koa2 workpackage 其他小的不计 前端Vue组件搭建 数据的简单测试交互 数据库的设计 创建.连接接数据库 前台[表单/分类] ...
- mysql使用数据库
哈哈 只能怪自己太菜哈 刚接触这个MySQL没多久 今天用终端登陆MySQL的时候mysql -u root -p 然后就想看看自己的数据库 我用的MySQL的客户端是navicat for mysq ...
- 在Notepad++中使用文本对比插件
目前Notepad++最新版是7.5.1,但很多插件仍然不能在64位版中使用,官网上是这么说的“Note that the most of plugins (including Plugin Mana ...
- php的基础知识(三)
12.函数: 函数的功能: 定义:在真实的项目开发过程中,有些代码会重复利用,我们可以把它提出来,做成公共的代码,供团队来使用,这个我们封装的代码段,就是函数(功能). 优点: 1.提高代码的利用率. ...
- python学习笔记二:if语句及循环语句,断点,模块,pyc
if语句 注意:语句块中的内容要强制缩进,否则出错.IndentationError,缩进错误 所有代码,如果是顶级的,必须顶格写,前面不能有空格 if … : … elif … : … else: ...
- (杭电 1702)ACboy needs your help again!
ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- 封装axios方法之一
一.先来说说为什么要封装axios异步请求. 我们前端开发中总是会遇到跨域的问题,我们会配置proxy来解决跨域的问题,无论是vue 还是react. 如何配置我这里就不说了. 然后...然后我们就会 ...
- CSS 兼容iPhone X、iPhone XS及iPhone XR
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ra ...