42.Flatten Binary Tree to Linked List
Level:
Medium
题目描述:
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
思路分析:
思路一:将二叉树转换为链表,我们可以先序遍历二叉树将节点保存起来,然后依次访问节点构造链表。
思路二:不使用额外的空间,在遍历的过程中构造链表。
代码:
代码一
/**public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}*/
public class Solution{
List<TreeNode>list=new ArrayList<>();
public void flatten(TreeNode root){
if(root==null)
return;
pre(root);
for(int i=0;i<list.size()-1;i++){
list.get(i).left=null;
list.get(i).right=list.get(i+1);
}
}
public void pre(TreeNode root){
if(root!=null){
list.add(root);
pre(root.left);
pre(root.right);
}
}
}
代码二
public class Solution{
TreeNode pre=null;
public void flatten(TreeNode root){
if(root==null)
return;
flatten(root.right);
flatten(root.left);
root.right=pre;
root.left=null;
pre=root;
}
}
42.Flatten Binary Tree to Linked List的更多相关文章
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- 31. 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 ...
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
- 【LeetCode】Flatten Binary Tree to Linked List
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...
- 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: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]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 e ...
- 【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 ...
随机推荐
- PHP内置函数parse_str会自动进行urldecode(URL解码)
用法:void parse_str ( string $str [, array &$arr] ) parse_str用来解析(分离)URL中的查询字符串(Query String),所谓查询 ...
- repquota - 文件系统配额的汇总
SYNOPSIS(总览) repquota [ -vugs ] filesystem... repquota [ -avugs ] DESCRIPTION(描述) repquota 显示与配额文件相关 ...
- smbmount - 装载一个 smbfs 文件系统
总览 SYNOPSIS smbmount {service} {mount-point} [-o options] 描述 DESCRIPTION smbmount 可以装载一个Linux SMB文件系 ...
- c++求中位数
#include <iostream> #include <cassert> #include <stack> #include <math.h> us ...
- java定义类
package java04; /* * 定义一个类,用来模拟“学生”,其中包含量两个组合部分 * * 格式: * public class ClassName{ * //成员变量 * //成员方法 ...
- create-react-app按需引入antd-mobile
1.引入 react-app-rewired 并修改 package.json 里的启动配置: npm i react-app-rewired@2.0.2-next.0 // 需要安装低版本 否则np ...
- python的strip和split函数
这两个函数都是string的类函数 1.strip是去掉字符串头尾的特定字符,分三个 aa=' bb=aa.rstrip(') cc=aa.lstrip(') dd=aa.strip(') print ...
- 10.VScode Debug——2019年12月12日
title: vscode debug date: "2019-09-17 16:17:16" tags: 技巧 categories: 技术驿站 1.为什么需要调试 写了很多行代 ...
- List和Tuple的中的method对比
- JS当中的无限分类递归树
列表转换成树形结构方法定义: //javascript 树形结构 function toTree(data) { // 删除 所有 children,以防止多次调用 data.forEach(func ...