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) {
TreeNode p=root;
List<TreeNode> list=PreOrder(root); for(int i=1;i<list.size();i++)
{
p.right=list.get(i);
p.left=null;
p=p.right;
} }
public List<TreeNode> PreOrder(TreeNode root){//树的前序遍历
List<TreeNode> list=new ArrayList<>();
try{
list.add(root); if(root.left!=null)
list.addAll(PreOrder(root.left));
if(root.right!=null)
list.addAll(PreOrder(root.right));
}catch(NullPointerException e){}
return list;
}
}
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. 将二 ...
- 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(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 【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 OJ 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]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- S1:new操作符
function Shape(type){ this.type = type || "rect"; this.calc = function(){ return "cal ...
- 继承自NSObject的类不能用CGRect
我用的是Xcode6.2. 系统默认没有pch文件. 所以没有自动导入UIKit包. 我在继承NSObject类里也不能用CGRect或者UI开头的控件,原因也是Xcode6.2以后版本 缺少UIKi ...
- JSONObject put,accumulate,element的区别(转载)
原文链接:http://ljhzzyx.blog.163.com/blog/static/3838031220126810430157/ public Object put (Object key ...
- Java Bad version number in .class file
错误信息: java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLo ...
- 后台获取不规则排列RadioButton组的值
获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList: <asp:RadioButtonList ID="rbl" runat=&quo ...
- 关于查询oracle in >1000 的讨论
https://q.cnblogs.com/q/88538/
- 【python练习】截取网页里最新的新闻
需求: 在下面这个网页,抓取最新的新闻,按天划分. http://blog.eastmoney.com/13102551638/bloglist_0_1.html 实现方法1:使用递归 import ...
- JS设计模式书籍、原则
圣经书:JavaScript 高级程序设计 设计模式:DesignPatterns by Erich Gamma.Richard Hlem.Ralph Johnson .Jhon Vlissides ...
- HTML---6 运算符,类型转换
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- C#中的五个访问修饰符
一.public, private, protected, internal, protected internal 1.public : 公开的, 公共的 2.private : 私有的 (只能在当 ...