[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
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.
题意:将任意一颗二叉树转换成一颗仅有左子树的二叉树。
要求:(1)不得使用额外存储空间,就地排序整理。
(2)修改之后的树为仅有左子树的二叉树,节点顺序为原二叉树的先序遍历。
思路:说是结果为先序遍历,但是递归方式为后序遍历方式。
class Solution {
public:
void flatten(TreeNode *root) {
if(root==NULL) return;
flatten(root->left);
flatten(root->right);
if(root->left==NULL) return;
TreeNode *p = root->left;
while(p->right) p=p->right;
p->right=root->right;
root->right=root->left;
root->left=NULL;
}
};
作者:Double_Win
出处: http://www.cnblogs.com/double-win/p/3875262.html
[LeetCode 题解]: Flatten Binary Tree to Linked List的更多相关文章
- 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-pl ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
- [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][JAVA] 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 (middle)
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 ----- java
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 ...
随机推荐
- 读《分布式一致性原理》zookeeper运维
1.配置详解 1.1基本配置 基本参数包括clientPort,dataDir和tickTime 1.2高级配置 下面我们再来看看zookeeper中一些高级配置参数的配置实用 2.四字命令 我们曾经 ...
- Delphi 拖动
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 ...
- 表单验证常用的JS正则表达式
在表单验证中,使用正则表达式来验证正确与否是一个很频繁的操作,本文收集整理了15个常用的javaScript正则表达式,其中包括用户名.密码强度.整数.数字.电子邮件地址(Email).手机号码.身份 ...
- script放置最佳位置(转载)
html文件是自上而下的执行方式,但引入的css和javascript的顺序有所不同,css引入执行加载时, 程序仍然往下执行,而执行到<script>脚本是则中断线程,待该script脚 ...
- 退出telnet 命令
很多时候 telnet 完就无法退出了,ctrl+c 有时也无法退出 后来找到了正确的命令 ctrl+] 然后在telnet 命令行输入 quit 就可以退出了
- 【原】Coursera—Andrew Ng机器学习—Week 5 习题—Neural Networks learning
课上习题 [1]代价函数 [2]代价函数计算 [3] [4]矩阵的向量化 [5]梯度校验 Answer:(1.013 -0.993) / 0.02 = 3.001 [6]梯度校验 Answer:学习的 ...
- AlienWare
https://www.chiphell.com/thread-1705089-1-1.html AlienWare
- Python tkinter 副窗体置顶和取消置顶
root = Tk() #置顶root.wm_attributes('-topmost',1) #取消置顶 root.wm_attributes('-topmost',0)
- Maven详解【面试+工作】 各种安装 没用
1 Maven介绍1.1 项目开发中遇到的问题 1.都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? 2.为什么在我的机器上可以正常打包,而配置管理员却打不出来? 3.项目组加入 ...
- 本地yum源构建以及Docker离线安装
Docker离线安装以及本地yum源构建 在docker的使用过程中有时候会遇到一些私有化部署的问题,就是在一些无法上网的机器上面安装使用dokcer,这就引出了docker的离线安装的问题,dock ...