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. php session的简单使用

    创建session: session_start(); $_SESSION['name'] = $value; 获取session: session_start(); echo $_SESSION[' ...

  2. 根据word模版导入word中用户填写的数据

    背景 客户有个需求:从word格式文档中读项目关键信息到数据库中,如:第一个表格中的联系人,项目名之类的信息,word中的格式不是固定的,可以会有些改动. 分析 方案1:读取第一个表格,然后再读取表格 ...

  3. (转)Linux-HA开源软件Heartbeat(配置篇)

    原文:http://ixdba.blog.51cto.com/2895551/548625 http://gzsamlee.blog.51cto.com/9976612/1828870 Linux-H ...

  4. Objective-C中.h、.m、.mm的区别

    .h :头文件.头文件包含类,类型,函数和常数的声明.  .m :源代码文件.这是典型的源代码文件扩展名,可以包含Objective-C和C代码.  .mm :源代码文件.带有这种扩展名的源代码文件, ...

  5. maven打包报错 ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id

    打开pom.xml 在build标签中 增加 <defaultGoal>compile</defaultGoal> 如下: <build><defaultGo ...

  6. java POST 传值 加签 验证

    话不多说,代码如下 package com.syl.test_key; import lombok.extern.slf4j.Slf4j; import org.apache.commons.code ...

  7. 异步http请求的实现

    这是我自己在某论坛上发的一篇水贴:http://www.sufeinet.com/thread-9275-1-2.html,原理和解释,我就直接重发一遍在自己博客上了. 时隔一个月  回来把之前的坑填 ...

  8. WebAPI搭建(二) 让WebAPI 返回JSON格式的数据

    在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...

  9. 工厂模式(Factory Pattern)

    一.工厂模式(Factory Pattern)的介绍 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使 ...

  10. springboot遇到的那些坑

    一.在springboot整合jsp时,程序中的所有配置都是正确的,但是在启动springboot后,访问无法找到jsp页面,报错404, 解决办法 二.在springboot整合jpa实现crud时 ...