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 ...
随机推荐
- c# 线程浅析(代理 、Invoke、Lock)
前言:本来想根据自己的经验总结一下c#线程相关的知识点, 写之前看了一些其他人的博客,发现自己也就掌握了不到三分之一....希望通过这次的博客将自己的知识点补充一下,写出更直白的博客和初学者分享. 这 ...
- 重新学习Java——对象和类(一)
之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...
- 百度地图API在vue-cli中路径错误的问题
在使用百度地图的时候,需要使用自定义的icon图片,百度的案例中使用的是线上地址,但当替换为本地图片路径的时候,错误出现了 这是本地图片地址 ) // 设置覆盖物大小 ); 这里有一点需要注意,这里路 ...
- <a>标签的href、onclick属性
链接的 onclick 事件被先执行,其次是 href 属性下的动作(页面跳转,或 javascript 伪链接): 参考:https://www.cnblogs.com/happykakeru/ar ...
- 【转】WITH AS 用法
转载自:http://blog.csdn.net/shaochao14/article/details/6223052 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery ...
- 03JavaScript运算符与表达式
JavaScript运算符与表达式 2.5运算符与表达式 2.5.1赋值运算符 运算符 意义 运算符 意义 = x=5 /= x=x/y += x=x+y %= 求余赋值 -= x=x-y *= x= ...
- javaScript--进阶1--数据类型、操作符
一.JS基础知识背景 1.1 弱类型脚本语言 脚本语言是:弥补编译语言的不足而存在的,作为补充语言,不用编译,解析一行执行一行. 弱类型语言:简单理解定义一个变量,可以有多种数据类型.(var tem ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- 牛客练习赛25 C 再编号
解题思路 我们先来观察一下题目中给出的公式 $$a'_i=(\sum_{j=1}^na_j)-a_i$$ 通过这个公式推一下经过再编号后的序列的总和,因为我们推出这个和之后可以进行下一次计算. $$\ ...
- Re0:DP学习之路 饭卡 HDU - 2546
解法 01背包变式,首先贪心的想一下如果要保证余额最小那么就需要用相减后最小的钱减去之前最大的价格,且得保证这个钱在5元以上 对于寻找如何减最多能包含在5元以上,这里用01背包 我们把价钱看做体积装进 ...