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

解题思路:

看上去很简单,使用二叉树的中序遍历就可以实现。但是题目的小曲点在于要在原tree上修改,如果将左儿子放在右儿子位置上,会丢失右子树,导致遍历失败。

解决方法有两种:

1、不使用多余空间,将右子树放在左子树中,最右边儿子的右儿子位置上,也正好满足了,leftchild -> data -> rightchild的中序顺序,但是缺点是要遍历多次树,时间复杂度高;

2、只关注左儿子,将左儿子放到右儿子的位置上,同时使用栈结构,将所有右子树挨个入栈。在没有左儿子时,取出栈中元素。

第二种解法的代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
stack<TreeNode*> rights;
TreeNode* node = root; while (node != NULL) {
while (node->left) {
if (node->right)
rights.push(node->right);
node->right = node->left;
node->left = NULL;
node = node->right;
} if (node->right) {
node = node->right;
continue;
} if (!rights.empty()) {
node->right = rights.top();
node = node->right;
rights.pop();
} else {
break;
}
} return;
}
};

【Leetcode】【Medium】Flatten Binary Tree to Linked List的更多相关文章

  1. Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)

    114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...

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

  3. 【LeetCode】114. 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 ...

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

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

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

  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-place. For e ...

  7. leetcode dfs Flatten Binary Tree to Linked List

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

  8. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; 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 ...

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

  10. 114. Flatten Binary Tree to Linked List(M)

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

随机推荐

  1. init_config_file.lua

    --[[ 读取限流配置 --]] --获取共享内存 local limit_req_store = ngx.shared.limit_req_store --初始化拒绝 URI 列表 reject_u ...

  2. Python批量修改文件名(os库)

    问题: 在某一文件夹内有97个sql文件,全部都以统一格式命名,例如“A201222-广州李小龙纪念协会-1-广州李小龙纪念协会-2018.AUD” 由于有两段重复了,而且中间的“1”也没有意义,需要 ...

  3. (转)linux内存源码分析 - 内存回收(lru链表)

    原文:http://www.cnblogs.com/tolimit/p/5447448.html 概述 对于整个内存回收来说,lru链表是关键中的关键,实际上整个内存回收,做的事情就是处理lru链表的 ...

  4. LVS+keepalived DR模式配置高可用负载均衡集群

    实验环境 LVS-Master 10.0.100.201 VIP:10.0.100.203 LVS-Slave       10.0.100.204 WEB1-Tomcat 10.0.2.29 gat ...

  5. RequireJs使用快速入门

    前言:Requirejs作为一个ES5环境流行的模块加载器,在很多项目中使用它.而且这个开源库任然在更新,同类产品seajs已经不更新了. ES6之后引入import 或者使用Commonjs的方式引 ...

  6. Hadoop实战之四~hadoop作业调度详解(2)

    这篇文章将接着上一篇wordcount的例子,抽象出最简单的过程,一探MapReduce的运算过程中,其系统调度到底是如何运作的. 情况一:数据和运算分开的情况 wordcount这个例子的是hado ...

  7. js获取span标签的值

    <!DOCTYPE html> <html lang="en"><head> <meta charset="UTF-8" ...

  8. Spring boot Mybatis 整合(注解版)

    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...

  9. Java-----思想认识

    1.1 Java封装性思想的初步理解 从项目需求的角度出发,建立各个模块化的类,各个模块暂时不需要具体的方法描述,只需要各个模块的理想到位. 以银行项目为例.初步设想需要三个模块:银行(Bank类). ...

  10. HDU 1561 The more, The Better 经典树形DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...