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 ...
随机推荐
- Ubuntu: an error occurred while mounting /mnt/hgfs
对于这个error,我采用的一个不完美的方法是: vi /etc/fstab .host:/projectname /mnt/hgfs vmhgfs rw,ttl=,uid=my_uid,gid=my ...
- javascript photo1
- C++-多重继承的注意点
1, 钻石型多重继承如果不想要底部的类有重复的变量,则需要声明为virtual继承 class File{...}; class InputFile: virtual public File{..}; ...
- HDU 1465
排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常 ...
- 从协议VersionedProtocol开始1
Phase 0: Make a plan You must first decide what steps you're going to have in your process. It sound ...
- [转]android Intent机制详解
转自:http://blog.csdn.net/t12x3456/article/details/7688154 1.什么是Intent Intent是一种运行时绑定(run-time binding ...
- Gmail新版截图曝光 你还能认得出来吗?
Gmail即将迎来巨大的改变.据外媒消息,目前Google正在测试新的网页版Gmail.要知道从Gmail推出以来还从未进行过如此大的改动. 新版Gmail中,界面相比之前,采用了更加扁平话的设计,整 ...
- Android Context
http://www.cnblogs.com/android100/p/Android-Context.html
- SVG 2D入门7 - 重用与引用
前面介绍了很多的图形元素,如果很多图形本身是一样的,需要每次都去定义一个新的么?我们能否共用一些图形呢?这是这节的重点 - SVG元素的重用. 组合 - g元素 g元素是一种容器,它组合一组 ...
- hdu 2054
Ps:WA了无数次,,简直成了心病..今天终于AC了..先取整数部分,去零,判断位数相等否,再比较.如果相等,再取小数部分,去零,比较,输出....好烦... 代码; #include "s ...