[LeetCode]Flatten Binary Tree to Linked List
2016-04-27 11:19
405 查看
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
click to show hints.
Subscribe to see which companies asked this question
题解:
code:
public class Solution {
public void flatten(TreeNode root) {
if( root != null)
{
flatten(root.right);
if(root.left != null)
{
flatten(root.left);
TreeNode node = root.left;
while(node.right != null)
{
node = node.right;
}
node.right = root.right;
root.right = root.left;
root.left = null;
}
}
}
}
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
click to show hints.
Subscribe to see which companies asked this question
题解:
code:
public class Solution {
public void flatten(TreeNode root) {
if( root != null)
{
flatten(root.right);
if(root.left != null)
{
flatten(root.left);
TreeNode node = root.left;
while(node.right != null)
{
node = node.right;
}
node.right = root.right;
root.right = root.left;
root.left = null;
}
}
}
}
相关文章推荐
- header头解决跨域请求
- pycaffe
- 《疯狂Java讲义(第3版)》.(李刚)——数组
- AJAX POST&跨域 解决方案 - CORS
- Android应用经典主界面框架之二:仿网易新闻客户端、CSDN 客户端 (Fragment ViewPager)
- BZOJ 2251.[2010Beijing Wc]外星联络(Trie)
- hibernate中使用mariaDB
- jQuery中 trigger() & bind() 使用心得
- C#设计模式--简单工厂
- FreeMarker自定义指令--代码实现
- 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
- Cocosd2d-js 实现窗体管理器
- 算法系列15天速成-第一天 七大经典序列(下)
- cacti 监控Linux服务器
- iOS 键盘的显示与关闭
- IT技术团队行而有效的管理之道
- [Java] Object类
- Kafka Zookeeper 基本命令示例
- cocos2dx lua 3.10 接入cjson
- mysql触发器的使用