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成 ...
随机推荐
- 攻防世界--ReverseMe-120
测试文件:https://adworld.xctf.org.cn/media/task/attachments/a5c0e8322d9645468befabddfe0cb51d.exe 1.准备 获取 ...
- 使用docxtemplater来处理word模板
工作中遇到需要根据不同数据来处理模板word的情况,网上搜索了一番之后,找到了一个叫做docxtemplater的库,使用起来非常便携,也十分满足此次的需求. 这次就来记录一下docxtemplate ...
- Solr的学习使用之(七)Solr高级查询facet、facet.pivot简介
以下转载自:http://hongweiyi.com/2013/03/apache-solr-facet-introduction/ 1.什么是Faceted Search Facet['fæsɪt] ...
- linux命令截取文件最后n行(所有命令)
linux命令截取文件最后n行(所有命令) tail -n a.txt > b.txt 联想:系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) una ...
- python常用函数 Z
zip(iterable, iterable..) 数据打包和解包,一般结果是一个元组(最短匹配). 例子:
- BZOJ2588 树上静态第k大
题意翻译 给你一棵有n个结点的树,节点编号为1~n. 每个节点都有一个权值. 要求执行以下操作: U V K:求从节点u到节点v的第k小权值. 输入输出格式 输入格式 第一行有两个整数n和m(n,m≤ ...
- 【Database】Mysql分布式集群学习笔记
一.sql 的基本操作 (2018年11月29日,笔记) (1)数据库相关操作 创建数据库.查看数据库.删除数据库 #. 创建数据库 create database mytest default ch ...
- shell getopts用法详解
本文链接:https://blog.csdn.net/u012703795/article/details/46124519 获取UNIX类型的选项: unix有一个优点就是标准UNIX命令在执行时都 ...
- PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患(转)
PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患 时间 2014-11-14 15:05:49 WooYun知识库 原文 http://drops.wooyun.org/t ...
- python 的三元运算符
一.三元运算符 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] res = 值1 if 条件 els ...