114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)
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
Hints:
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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
stack<TreeNode*> s;
TreeNode *p = root, *pre = NULL;
while(p || !s.empty())
{
while(p)
{
if(pre)
{
pre->right = p;
pre = NULL;
}
if(!p->left && !p->right)
pre = p;
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
p = p->right;
}
}
for(p = root; p; p = p->right)
{
if(p->left)
{
p->right = p->left;
p->left = NULL;
}
}
}
};
114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)的更多相关文章
- 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 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [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 ...
- [LeetCode] 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 ...
- 114. Flatten Binary Tree to Linked List 把二叉树变成链表
[抄题]: Given a binary tree, flatten it to a linked list in-place. For example, given the following tr ...
- [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...
- 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. 将二 ...
随机推荐
- PL/SQL Developer登入时候报ORA-12638
在client安装目录,找到打开sqlnet.ora 在里面找到 SQLNET.AUTHENTICATION_SERVICES= (NTS)将其更改为: SQLNET.AUTHENTICATION_S ...
- 利用gulp解决微信浏览器缓存问题
做了好多项目,这次终于要解决微信浏览器缓存这个令人头疼的问题了.每次上传新的文件,在微信浏览器中访问时,总要先清除微信的缓存,实在麻烦,在网上搜罗了很多解决办法,终于找到了方法:利用gulp解决缓存问 ...
- talib 中文文档(七):Overlap Studies Functions
Overlap Studies Functions 重叠指标 BBANDS - Bollinger Bands 函数名:BBANDS 名称: 布林线指标 简介:其利用统计原理,求出股价的标准差及其信赖 ...
- Day22 文件上传下载和javaMail
day22总结 文件上传概述 1 文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢. 2 文件上传对页面的要求 上传文件的要求比较多,需要 ...
- 把大象装进冰箱的N种方法
作者:折剑头链接:https://www.zhihu.com/question/49214119/answer/115728034来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- 【spring mvc】扒一扒tomcat
1.TOMCAT的目录 主要说一下webapps目录,WEB应用的目录结构:假设在$CATALINA_HOME/webapps下有helloapp的web应用 /helloapp:Web应用的根目录, ...
- Java-idea-常用技巧-转maven,解决包依赖冲突
1.Intellij IDEA如何将普通工程转换成maven工程 项目上右键 Add Framework Support,选择maven 2.Intellij IDEA 自动生成 serialVers ...
- oracel 服务详细介绍
中的方法成功安装Oracle 11g后,共有7个服务, 这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Service: Oracle卷映射拷贝写入服务,VSS(Volu ...
- libgomp-4.8.5-28.el7_5.1.x86_64 is a duplicate with libgomp-4.8.5-4.el7.x86_64
列出重复的包 # package-cleanup --dupes 删除重复的包 # package-cleanup --cleandupes
- 5.1 Components — Introduction
1. HTML被设计的时候,浏览器是一个简单的文件浏览器.开发构建大的Web应用程序需要更多的东西. 2. 不是试图取代HTML,然而,Ember.js拥抱它,然后增加了许多新功能使得构建web应用程 ...