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 ...
随机推荐
- struts2中<s:if>标签的使用
转载:http://blog.sina.com.cn/s/blog_5f9938640100v2kr.html A:<s:if>判断字符串的问题: 1.判断单个字符:<s:if te ...
- YCSB-mapkeer-leveldb实测
使用thrift0.8.0编译好java版的mapkeeper并安装到ycsb下,使用thrift0.9.2编译好c++版的mapkeeper并编译leveldb客户端运行. 测试成功.recordc ...
- Yii的缓存机制之数据缓存
具体说法就是可以缓存变量信息. 设置:Yii::app()->cache->set(名字, 值, 过期时间): 使用:Yii::app()->cache->get(名字); 删 ...
- JAVA基础细谈
JAVA基础细谈 一. 源文件和编译后的类文件 源文件的本质就是程序文件,是程序员编写,是人看的.而编译后的类文件是给电脑看的文件.一个类就是一个文件,无论这个类写在哪里,编译以后都是一个文件 ...
- BZOJ_4154_[Ipsc2015]Generating Synergy_KDTree
BZOJ_4154_[Ipsc2015]Generating Synergy_KDTree Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点 ...
- 测试build出来的dist文件夹是否编译成功
一.先用webpack执行 npm run build 成功后会生成dist文件夹. 二.把dist文件夹推到SVN项目指定位置.注意:因为build后会生成很多的js css font文件并没用加入 ...
- bzoj 5092 分割序列 —— 高维前缀和
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5092 首先,处理出异或前缀和 s[i],i 位置的答案就是 s[j] + s[j]^s[i] ...
- 《Kubernetes权威指南第2版》学习(二)一个简单的例子
1: 安装VirtualBox, 并下载CentOS-7-x86_64-DVD-1708.iso, 安装centOS7,具体过程可以百度. 2:开启centOS的SSH, 步骤如下: (1) yum ...
- ubuntu下使用锐捷校园网
前言 以下内容是个人学习之后的感悟,转载请注明出处~ 1.首先下载锐捷Linux版本,然后解压缩后,有个rjsupplicant.sh这个脚本文件,于是按照README做了,终端中 ...
- dubbo 学习笔记 -- provider端
服务端的配置文件: provider.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...