[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 example,
Given1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
这题的主要难点是“in-place”。因为这个flatten的顺序是中、左、右(相当于中序遍历),所以右子树总是在最后的,所以解题思想就是,每次把当前节点的右子树放到左子树的最右边。
图示例:
c++实现代码如下:
/**
* 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:
vector<int> re;
void flatten(TreeNode* root) {
TreeNode *now = root;
while(now){
if(now->left){
TreeNode *pre = now->left;
//找到左子树的最右节点
while(pre->right){
pre = pre->right;
}
//now的右节点放到左子树的最右节点
//now的右节点更新为左节点,左节点赋为NULL
pre->right = now->right;
now->right = now->left;
now->left = NULL;
}
//now不断向右向下
now = now->right;
}
return;
}
};
[LeetCode]Flatten Binary Tree to Linked List题解(二叉树)的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [leetcode]Flatten Binary Tree to Linked List @ Python
原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...
- [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 ...
- [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 - 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 ...
随机推荐
- python寻找list中最大值、最小值并返回其所在位置
c = [-10,-5,0,5,3,10,15,-20,25] print c.index(min(c)) # 返回最小值 print c.index(max(c)) # 返回最大值
- openstack 创建虚拟机的时候报错: Failed to allocate the network(s), not rescheduling.].
错误: 实例 "test-gtj" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Build of instance 5ea8c935-ee07-4788-823 ...
- You can also run `php --ini` inside terminal to see which files are used by PH P in CLI mode
在php.ini中打开extension=php_fileinfo.dll 就可以了
- 《JAVA与模式》之桥梁模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述桥梁(Bridge)模式的: 桥梁模式是对象的结构模式.又称为柄体(Handle and Body)模式或接口(Interface)模式. ...
- innodb 源码分析 --锁
innodb引擎中的锁分两种 1)针对数据结构, 如链表 互斥锁 读写锁 http://mysqllover.com/?p=425 http://www.cnblogs.com/justfortast ...
- CentOS7搭建FastDFS V5.11分布式文件系统-第三篇
1.测试 前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解.FastDFS的基础模块都搭好了,现在开始测试下载. 1.1 配置客户端 同样的,需要修改客户端的配置文件: /etc/fdf ...
- Ejb3 + Jboss8 出现Session id hasn't been set for stateful component
Ejb 3 + JBoss 8 在使用客户端远程访问有状态的Ejb对象时,出现ERROR: Session id hasn't been set for stateful component 出现该 ...
- 一分钟让你学会使用Android AsyncTask
AsyncTask相信大多数朋友对它的用法都已经非常熟悉,这里记录一下主要是献给那些刚刚接触的Android 或者AsyncTask的同学们,高手请绕道. AsyncTask类是Android1.5版 ...
- 快速搭建gulp项目实战
gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码,而且大 ...
- vscode 常用配置
{ "workbench.iconTheme": "vscode-icons", "editor.tabSize": 2, "ed ...