【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 example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
前序遍历,按访问序往右加就行了。
需要注意的是,加入单链之后要删掉原先的子女链接。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode *root) {
if(!root)
return; TreeNode* newtail = new TreeNode(-);
stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty())
{
TreeNode* top = stk.top();
newtail->right = top;
newtail = newtail->right;
stk.pop(); if(top->right)
stk.push(top->right);
if(top->left)
stk.push(top->left); top->left = NULL;
top->right = NULL;
}
}
};

【LeetCode】114. Flatten Binary Tree to Linked List的更多相关文章
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- 【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 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】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 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. 将二 ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 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 ...
随机推荐
- Codeforces Round #301 (Div. 2) C. Ice Cave BFS
C. Ice Cave Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/problem/C ...
- Scrapy入门教程(转)
关键字:scrapy 入门教程 爬虫 Spider作者:http://www.cnblogs.com/txw1958/出处:http://www.cnblogs.com/txw1958/archive ...
- IDA64 Fatal error before kernel init
http://www.tuicool.com/articles/7FZVZna 第一次看到这个错误还以为是修改文件导致的,但是觉得又不大像,因为在Win7底下是完全正常的.搜索了一下才发现是由于插件导 ...
- 面试题1:如何实现C++单例模式?
1. 软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多态.真正领悟设计模式的精髓是可能一个漫长的过程,需要大量实践经验的积 ...
- 程序猿怎样变身IT讲师
我以前写过一篇文章,"一张图道尽程序猿的出路",里面有一张图: 这张图随着"一张图道尽程序猿的出路"这篇文章,以前被伯乐在线.docin(豆丁网).IT面试.J ...
- 菜鸟学Java(二十)——你知道long和Long有什么差别吗?
Java中数据类型分两种: 1.基本类型:long,int,byte,float,double2.对象类型:Long,Integer,Byte,Float,Double其他一切java提供的,或者你自 ...
- java自动识别上传的apk版本号
import java.util.List; public class ApkInfo { private String versionCode; private String versionName ...
- Android如何检查对象的类型
The instanceof operator compares an object to a specified type. You can use it to test if an object ...
- Java中的break和continue关键字使用总结
java中的break和continue关键字使用总结 一.作用和区别 break的作用是跳出当前循环块(for.while.do while)或程序块(switch).在循环块中的作用是跳出 ...
- Spring3之InternalResourceViewResolver
打开Spring的源代码,我们可以在org.springframework.web.servlet.view包下看到很多的 View和ViewResolver类;View类为我们提供一些缺省的待扩展的 ...