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
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
思路:这题主要是就是前序遍历。主要解法就是将左子树转换为右支树,同一时候加入在右子树前。左子树求解时,须要主要左子树的深度。
详细代码例如以下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
if(root == null){
return;
} TreeNode r = new TreeNode(0);
fun(root,r);
root = r.right;
} private boolean fun(TreeNode root, TreeNode t){
if(root == null){
return false;
}
TreeNode p = root.left;
TreeNode q = root.right; t.right = root;
t.left = null; if(p != null){
if(fun(p, t.right)){
if(q != null){
TreeNode k = t.right;
while(k.right != null){
k = k.right;
}
fun(q,k);
}
}else{
if(q != null){
fun(q,t.right);
}
}
}else{
if(q != null){
fun(q,t.right);
}
}
return true;
}
}
leetcode 114.Flatten Binary Tree to Linked List (将二叉树转换链表) 解题思路和方法的更多相关文章
- [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- [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 ...
- [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 ----- java
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_Medium tag: DFS
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- Java for 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 ...
随机推荐
- Spring Boot (29) 定时任务
使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...
- [ Luogu 4626 ] 一道水题 II
\(\\\) \(Description\) 求一个能被\([1,n]\) 内所有数整除的最小数字,并对 \(100000007\) 取模 \(N\in [1,10^8]\) \(\\\) \(Sol ...
- 三维重建:GitHub百度Apollo 2.0
GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...
- 【笔记JS/HTML/CSS】ubuntu环境下的sublime text2 安装 zenCoding
刚接触web编程的时候就被老师安利了sublime text2 这个文本编辑器,后来发现它真的挺好用的,无论是windows还是ubuntu,都可以很简单地下载安装(到官网,免费哦),三分钟内就搞定了 ...
- 【笔记JS/HTML/CSS】用div实现个性化button,背景半透明
html中的button默认样式..不太能看,如果调一调背景色和字体的话也挺适合简洁的页面设计 于是决定配合JS,用html中的div完成button 最终结果图: html代码:(first_pas ...
- swift Hashable Equatable
/// You can use any type that conforms to the `Hashable` protocol in a set or /// as a dictionary ke ...
- margin与padding如何进行区分
margin与padding如何进行区分,这是很多学html人的困扰,其实说白了padding 就是内容与边框的空隙.而margin则是模块与模块的空隙.[3]
- c++写入txt
用ofstream 输出流,#include <fstream> ofstream outf; outf.open("abc.txt");outf<<123 ...
- JavaScipt30(第六个案例)(主要知识点:给数字加千分号的正则)
承接上文,这是第6个案例: 附上项目链接: https://github.com/wesbos/JavaScript30 这个主要是要实现在给定的json里匹配出搜索框里的city or state, ...
- 16位/32位/64位CPU的位究竟是说啥
平时,我们谈论CPU,都会说某程序是32位编译,可以跑在32位机或64位机,或则是在下载某些开源包时,也分32位CPU版本或64CPU位版本,又或者在看计算机组成相关书籍时,特别时谈到X86 CPU时 ...