把二叉树先序遍历,变成一个链表,链表的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 动态演示的更多相关文章

  1. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

随机推荐

  1. (五:NIO系列) Reactor模式

    出处:Reactor模式 本文目录 1. 为什么是Reactor模式 2. Reactor模式简介 3. 多线程IO的致命缺陷 4. 单线程Reactor模型 4.1. 什么是单线程Reactor呢? ...

  2. java 标识接口的作用

    标识接口的作用 标识接口是没有任何方法和属性的接口.标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型. 标接口在Java语言中有一些很著名的应用,例如我们常用的Arra ...

  3. python学习第四十六天dir( )函数用法

    dir( )函数有点像目录的意思,但是他是包含由模块定义的名称的字符串的排序列表.这个列表包含模块中定义的所有模块,变量和函数的名称. 列举其用法 import time content = dir( ...

  4. Spark2.0集成Hive操作的相关配置与注意事项

    前言 已完成安装Apache Hive,具体安装步骤请参照,Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作 补充说明 Hive中metastore(元数据存储)的三种方式 ...

  5. select 项目<选课系统>

    """1. 创建北京.上海 2 所学校 学校类对象 属性:名字,地址 行为: 2. 创建linux , python , go 3个课程 , linux\py 在北京开, ...

  6. MYSQL学习笔记——常用语句

    1.检索数据 1.1.检索单个列:SELECT prod_name FROM products; 1.2.检索多个列:SELECT prod_id, prod_name, prod_price FRO ...

  7. [SDOI2011]消防(贪心,图论,树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

  8. man(2) readv writev

    #include <sys/uio.h> ssize_t readv(int fd, const struct iovec *iov, int iovcnt); unix高级环境编程中的定 ...

  9. vue2.0  之 directive指令 (自定义)

    指令 一.定义: 指令只一种可以附加到DOM元素的微命令(tiny commands). 它们通常以"v-"作为前缀, 以方便Vue知道你在使用一种特殊的标记, 从而确保语法的一致 ...

  10. phpstorm 生产php pojo类

    一. 修改Generate POJO.groovy文件 改为 import com.intellij.database.model.DasTable import com.intellij.datab ...