题目:

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

代码:

/**
* 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) {
if (!root) return;
stack<TreeNode *> sta;
TreeNode *dummy = new TreeNode(INT_MIN);
TreeNode *pre = dummy;
dummy->right = root;
sta.push(root);
while ( !sta.empty() )
{
TreeNode *tmp = sta.top(); sta.pop();
if ( tmp->right ) sta.push(tmp->right);
if ( tmp->left ) sta.push(tmp->left);
tmp->left = NULL;
pre->right = tmp;
pre = tmp;
}
}
};

tips:

先序遍历(node->left->right);每次出栈的元素都切断left(为什么right不用切?因为在下一次迭代的时候,pre->right自然就把right的值覆盖了)。

设立一个pre节点,保存上一次出栈的元素。

pre->right = tmp就能按照题意把原有的tree给flatten了。

===================================

学习了一个递归版的代码

/**
* 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) {
// terminal condition
if (!root) return;
// recersive left and child
flatten(root->left);
flatten(root->right);
// if left is not null then do the reconnection
if (!root->left) return;
TreeNode *p = root->left;
while ( p->right) p = p->right;
p->right = root->right;
root->right = root->left;
root->left = NULL;
}
};

===================================================

第二次过这道题,使用非递归版做的,一开始忘记了给left方向断开,改了一次之后AC了。

/**
* 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*> sta;
TreeNode* pre = new TreeNode();
if ( root ) sta.push(root);
while ( !sta.empty() )
{
TreeNode* tmp = sta.top();
sta.pop();
pre->right = tmp;
pre->left = NULL;
if ( tmp->right ) sta.push(tmp->right);
if ( tmp->left ) sta.push(tmp->left);
pre = tmp;
}
}
};

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

  1. 【遍历二叉树】11把二叉树转换成前序遍历的链表【Flatten Binary Tree to Linked List】

    本质上是二叉树的root->right->left遍历. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  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-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】

    [114-Flatten Binary Tree to Linked List(二叉树转单链表)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

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

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

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

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

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

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

随机推荐

  1. zabbix介绍

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix组件主要分两个: zabbix-server和zabbix-agent.支持的监控协议有ICM ...

  2. KMP串匹配算法解析与优化

    朴素串匹配算法说明 串匹配算法最常用的情形是从一篇文档中查找指定文本.需要查找的文本叫做模式串,需要从中查找模式串的串暂且叫做查找串吧. 为了更好理解KMP算法,我们先这样看待一下朴素匹配算法吧.朴素 ...

  3. 深入浅出MongoDB(二)概述

    上次的博文深入浅出MongoDB(一)NoSQL中我们已经简单介绍了一下NoSQL的基本概念,这次我们来了解一下MongoDB的相关概念. 1.简介 MongoDB是一款由C++编写的高性能.开源.无 ...

  4. WordPress 主题开发 - (七) 让主题更安全 待翻译

    We're just about ready to start building our theme's template files. Before we do this, however, it' ...

  5. ubuntu上搭建vsftpd且通过mysql来管理FTP账号

    参考文章:http://wiki.ubuntu.org.cn/Vsftpd%E5%92%8Cmysql%E9%85%8D%E7%BD%AE 请各位先按照这篇文章一步一步操作,我这里是记录一些其间遇到的 ...

  6. recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组

    //递归返回字符串键名全为小写或大写的数组function recurse_array_change_key_case(&$input, $case = CASE_LOWER){    if( ...

  7. 如何将两个列表变成一个python字典

    一个列表是 index = [0, 1, 2, 3, 4, 5, 6] 另一个是 day = ['1', '2', '3', '4', '5', '6', '7' ] 可以使用dict(zip(ind ...

  8. 关于Raw,Assets的使用

    Raw,Assets下文件区别: 相同点:两个目录下的文件在打包后都会原封不动的保存到apk中,不会被编译成二进制. 不同点:Raw下文件不能使用目录结构, 有些格式的会被压缩,能够通过R.raw方便 ...

  9. ASP.NET MVC5学习笔记之Action参数模型绑定值提供体系

    这一节我们关注模型绑定的值提供体系,先来介绍几个重要的接口 一. IValueProvider,接口定义如下: public interface IValueProvider { bool Conta ...

  10. PHP 只有登陆后才能浏览的简单实现

    1.============================================================= 在你不想让别人直接进入的网页开头加一段代码: session_start ...