【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 ...
随机推荐
- Linux使用C语言链接MsSQL
1.安装gcc编译器 yum install gcc 2.下载freetds wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched ...
- 发现一个可以搜索常用rpm包的地址(http://www.rpmfind.net/)
http://www.rpmfind.net/ 虽然资源不多,但也够用.
- When to use static method in a java class
First , please understand its feature : * no need to instantiate a instance, i.e. simply you can jus ...
- 64位系统下同时使用64位和32位的eclipse
eclipse.ini 文件使用说明 The -vm option and its value (the path) must be on separate lines. The value must ...
- python核心模块之pickle和cPickle解说
pickle模块使用的数据格式是python专用的,而且不同版本号不向后兼容,同一时候也不能被其它语言说识别.要和其它语言交互,能够使用内置的json包使用pickle模块你能够把Python对象直接 ...
- 3DShader之法线贴图(normal mapping)
凹凸贴图(bump mapping)实现的技术有几种,normal mapping属于其中的一种,这里实现在物体的坐标系空间中实现的,国际惯例,上图先: 好了讲下原理 可以根据高度图生成法线量图,生成 ...
- 在w7下的wamp中配置memcache
php版本是5.4.16 ,我的电脑是w7 64位的. 一. memcache和memcached的区别 在自己的新程序中打算全面应用memcached技术,这个很容易理解这是memcached是内 ...
- C/C++之回调函数
转自:http://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html 今天讨论下C/C++中的回调函数. 在理解“ ...
- Android 获得view的宽和高
转自:http://blog.csdn.net/yangdeli888/article/details/25405263 在oncreate()中利用view.getWidth()或是view.get ...
- [图像]用Matlab在图像上画矩形框
原创文章,欢迎转载.转载请注明:转载自 祥的博客 原文链接:http://blog.csdn.net/humanking7/article/details/46819527 在毕业设计的时候写论文画图 ...