114 Flatten Binary Tree to Linked List 二叉树转换链表
给定一个二叉树,使用原地算法将它 “压扁” 成链表。
示例:
给出:
1
/ \
2 5
/ \ \
3 4 6
压扁后变成如下:
1
\
2
\
3
\
4
\
5
\
6
提示:
如果您细心观察该扁平树,则会发现每个节点的右侧子节点是以原二叉树前序遍历的次序指向下一个节点的。
详见:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/
Java实现:
递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if(root==null){
return;
}
if(root.left!=null){
flatten(root.left);
}
if(root.right!=null){
flatten(root.right);
}
TreeNode tmp=root.right;
root.right=root.left;
root.left=null;
while(root.right!=null){
root=root.right;
}
root.right=tmp;
}
}
非递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if(root==null){
return;
}
TreeNode cur=root;
while(cur!=null){
if(cur.left!=null){
TreeNode p=cur.left;
while(p.right!=null){
p=p.right;
}
p.right=cur.right;
cur.right=cur.left;
cur.left=null;
}
cur=cur.right;
}
}
}
python实现:
参考:https://www.cnblogs.com/grandyang/p/4293853.html
114 Flatten Binary Tree to Linked List 二叉树转换链表的更多相关文章
- 114 Flatten Binary Tree to Linked List [Python]
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- 114. Flatten Binary Tree to Linked List 把二叉树变成链表
[抄题]: Given a binary tree, flatten it to a linked list in-place. For example, given the following tr ...
- [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
随机推荐
- CSU - 1529 Equator —— DP 最大连续和子序列
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1529 题解: 一个加强版的最大连续和子序列,序列可以从末尾元素转到首元素. 分两种情 ...
- js程序开发-3
<h1>Date()类型</h1> 获取日期和时间 getDate() 获取日 1-31 getDay () 获取星期 0-6 getMonth () 获取月 0-11 get ...
- hadoop运行测试命令遇到的问题
2017-02-16 09:46:14,926 INFO mapreduce.Job: Task Id : attempt_1487148856575_0001_m_000001_0, Status ...
- hdu-5748 Bellovin(LIS)
题目链接: Bellovin Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- python获取系统信息psutil
python获取系统信息psutil:psutil获取系统cpu使用率的方法是cpu_percent(),其有两个参数,分别是interval和percpu,interval指定的是计算cpu使用率的 ...
- No result defined for action cn.crm.action.LinkManAction and result input
这是struts2的一个拦截器报的错误,当你的form中的数据有问题,比如说<input type="text" name="receiverLoginID&quo ...
- Python框架下django 的并发和多线程
django 的并发能力真的是令人担忧,django本身框架下只有一个线程在处理请求,任何一个请求阻塞,就会影响另一个情感求的响应,尤其是涉及到IO操作时,基于框架下开发的视图的响应并没有对应的开启多 ...
- button的FlatStyle和FlatAppearance属性
FlatStyle是处理边框的样式,而FlatAppearance是用来设置边框的颜色,宽度和鼠标移动和点击时的效果设置FlatStyle为Flat,并且设置FlatAppearance下的Borde ...
- 子组件索引$refs
$refs只在组件渲染完成之后才填充,并且它是非响应式的,它仅仅作为一个直接访问访问子组件的应急方案-----应当避免的模板中或者计算属性中使用$refs
- 在JS中将指定表单内的“具有name数据的表单元素的值”封装为Get形式的字符串
//封装post时候,表单中所有具有name数据的表单元素的值,并返回“n=1&p=a” function serialize(formid) { var arr = []; var ipts ...