给定一个二叉树,原地将它展开为链表。

例如,给定二叉树

1 / \ 2 5 / \ \ 3 4 6

将其展开为:

1 \ 2 \ 3 \ 4 \ 5 \ 6

class Solution {
public:
TreeNode *last = NULL;
void flatten(TreeNode* root)
{
if(root == NULL)
return;
TreeNode *r = root ->right;
if(last != NULL)
{
last ->right = root;
}
last = root;
flatten(root ->left);
flatten(r);
root ->left = NULL;
}
};

Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表的更多相关文章

  1. 114 Flatten Binary Tree to Linked List 二叉树转换链表

    给定一个二叉树,使用原地算法将它 “压扁” 成链表.示例:给出:         1        / \       2   5      / \   \     3   4   6压扁后变成如下: ...

  2. LeetCode114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. (Medium) For example,Given 1 / \ 2 5 / \ ...

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

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

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

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

  6. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

  7. leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  8. 114 Flatten Binary Tree to Linked List [Python]

    114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...

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

随机推荐

  1. java笔试之字符逆序(二)

    与字符逆序(一)不同,句子逆序,单词顺序.例如:I am a student 输出为student a am I. 想法:先字符串句子逆序,然后针对每个单词再逆序一遍. package test; i ...

  2. MFC注册热键

    注册热键. 当用户点击注册的快捷键时,做出相应的响应. 定义 ALT+M键为测量按钮响应函数: 头文件中定义: #define ID_HOTKEY1 10001 在初始化函数中加入注册热键函数: if ...

  3. angular 级联选择

    HTML: <link rel="stylesheet" href="views/tree/checkbox.css"/> <div clas ...

  4. 2、docker镜像管理

    Docker镜像管理 镜像是Docker容器的基础,想运行一个Docker容器就需要有镜像.我们上面已经学会了使用search搜索镜像.那么这个镜像是怎么创建的呢? 创建镜像 镜像的创建有以下几种方法 ...

  5. Linux-c 线程锁

      typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...

  6. Algo: Dynamic programming

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  7. string、char* 、int数据类型相互转换

    string类型转换成char*类型,这里一般有以下三种方法: 1.c_str()方法 string name="Qian"; char *str=(char*)name.c_st ...

  8. pixi.js 学习

     事件(event):PIXI库在精灵和舞台上提供了事件,用于交互. stage.click = function(data){ var event = data.originalEvent } sp ...

  9. Java-slf4j:sfl4j

    ylbtech-Java-slf4j:sfl4j 1.返回顶部 1. SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种 ...

  10. GC 案例收集整理

    1.数组动态扩容  现象:系统一直在做cms gc,但是老生代一直不降下去,但是执行一次jmap -histo:live之后,也就是主动触发一次full gc之后,通过jstat -gcutil来看老 ...