LeetCode 114. Flatten Binary Tree to Linked List 动态演示
把二叉树先序遍历,变成一个链表,链表的next指针用right代替
用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素
class Solution {
public:
void helper(TreeNode* cur, TreeNode*& tail){
//a(tail)
//lk("root",tail)
//a(cur)
//lk("root",cur)
//dsp
tail=cur;
TreeNode* right=cur->right;
//a(right)
//lk("root",right)
if(cur->left){
TreeNode *leftTail=NULL;
helper(cur->left, leftTail);
cur->right=cur->left;
cur->left=NULL;
tail=leftTail;
//dsp
}
if(right){
TreeNode *rightTail=NULL;
helper(right, rightTail);
tail->right=right;
tail=rightTail;
//dsp
}
}
void flatten(TreeNode* root) {
if(!root)
return;
//ahd(root)
TreeNode *tail=NULL;
helper(root, tail);
}
};
程序运行动态演示:http://simpledsp.com/FS/Html/lc114.html
LeetCode 114. 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] 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 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 ...
- [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- Java for 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 ...
- 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 ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
随机推荐
- python-docx 添加表格时很慢的解决方法
我们做监控系统的时候常需要给客户发送邮箱报告,附带一个word的文档,文档中插入表格给用户更直观的数据. 我用的时python-docx库操作文档,最近碰到,当往文档中插入表格时,随着表格行数的增多, ...
- 6、 逻辑回归(Logistic Regression)
6.1 分类问题 在分类问题中,你要预测的变量
- Python之字符串和正则表达式
使用正则表达式 Python对正则表达式的支持 例子:替换字符串中的不良内容 import re def main(): sentence = '你丫是傻叉吗? 我操你大爷的. Fuck you.' ...
- 2019牛客暑期多校训练营(第七场) - C - Governing sand - 平衡树
5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 感觉该出14才对,取前k小写成了取前k大. 5 1 5 4 2 5 3 3 5 2 4 5 1 6 5 5 suf=55 res=0 a ...
- SQLServer查看及设置最大连接数
很多时候自己本地开发会遇到 ,打开几个连接正常访问 之后就报错误,这时候需要调整sqlserver 最大连接数. 1. 查询最大连接数 SELECT value_in_useFROM sys.conf ...
- 问题 C: 序列交换
问题 C: 序列交换 时间限制: 1 Sec 内存限制: 128 MB提交: 914 解决: 48[提交] [状态] [命题人:jsu_admin] 题目描述 给一个 1 到 n 的排列,每次可以 ...
- Xilinx源语-------FDRE
1.源语---FDRE FDRE代表一个单D型触发器,含的有五个信号分别为: 数据(data,D).时钟使能(Clock enable,CE).时钟(Clock).同步复位(synchronous ...
- Python2 安装教程
目录 1. 推荐阅读 2. 安装包下载 3. 安装步骤 1. 推荐阅读 Python基础入门一文通 | Python2 与Python3及VSCode下载和安装.PyCharm破解与安装.Python ...
- pg_dumpall - 抽出一个 PostgreSQL 数据库集群到脚本文件中
SYNOPSIS pg_dumpall [ option...] DESCRIPTION 描述 pg_dumpall 是一个用于写出("转储")一个数据库集群里的所有 Postgr ...
- cd 切换目录
1. 功能说明 cd是“change directory”中每个氮气的首字母缩写功能是重当前工作目录切换到指定的工作目录:cd是内建命令. 2. 语法格式 cd [option] [dir] cd ...