【树】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.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var flatten = function(root) {
if(root==null){
return;
}
var stack=[],pre=null;
stack.push(root); while(stack.length!=0){
var p=stack.pop();
if(pre!=null){
pre.right=p;
pre.left=null
}
if(p.right){
stack.push(p.right);
}
if(p.left){
stack.push(p.left);
}
pre=p;
} pre.left=null;
pre.right=null;
};
【树】Flatten Binary Tree to Linked List(先序遍历)的更多相关文章
- 【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 ...
- [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: ...
- 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 ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
随机推荐
- js函数预编译
function fn(a){ console.log(a); var a = 123; function a(){} console.log(a); var b = function(){} con ...
- JavaNIO学习一
文章来源:http://cucaracha.iteye.com/blog/2041847 对文件来说,可能最常用的操作就是创建.读取和写出.NIO.2 提供了丰富的方法来完成这些任务.本文从简单的小文 ...
- Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- (转)mmap和shm共享内存的区别和联系
共享内存的创建 根据理论: 1. 共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制.共享内存可以通过mmap()映射普通文件 (特殊情况下还可以采用 ...
- 转:getContextPath、getServletPath、getRequestURI的区别
假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下结果 ...
- JS里关于特殊字符的转义
重定向的url里含有百分号“%”,遇到了apache 找不到该文件的报错.通过查询相关文档,知道了原来是url里含有特殊字符要转码才能定位到正确的地址.比如"%"要转码为" ...
- 浏览器报ScriptResource.axd异常
新拷贝的一份管理后台代码,部署在另一台服务器上时,查看浏览器调控制台,发现有几个红色报错.这些错误导致网站的部分功能无法使用. 主要错误有: 1.“Sys”未定义 2.asp.net ajax 客户端 ...
- vux Tabbar组件入门备忘大佬多指点
一.Tabbar引入及使用 1.新创建vue文件 2.在vue文件中添加插件 <template> <div> <tabbar style="position: ...
- Kotlin 初体验
本文由作者邹丽萍授权网易云社区发布. 背景 Kotlin 是 JetBrains 公司(著名的 IntelliJ IDEA 正是由这家公司开发的,Android Studio 也是基于 IDEA 的) ...
- 873. Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...