题目:

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. Linux ed命令

    $ ed              <- 激活 ed 命令  a                 <- 告诉 ed 我要编辑新文件  My name is Titan. <- 输入第 ...

  2. vs C#数据库导入EXCLE

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. MapReduce框架Partitioner分区方法

    前言:对于二次排序相信大家也是似懂非懂,我也是一样,对其中的很多方法都不理解诶,所有只有暂时放在一边,当你接触到其他的函数,你知道的越多时你对二次排序的理解也就更深入了,同时建议大家对wordcoun ...

  4. DNS笔记 DNS区域集成到 Active Directory

    可以将 DNS 区域集成到 Active Directory 中以提供增强的容错功能和安全性.OpenDNS   Google Public DNS往返时间 (RTT) 远程访问服务 (RAS)域名与 ...

  5. mac 下 svn ignore 操作

    如何在svn中设备忽略的文件或者文件夹 1.如果你还没有对你的文件夹进行版本控制,则可以直接图形操作上进行ignore,或者在命令中运行 svn propedit svn:ignore 文件夹名 . ...

  6. 有关c#装箱和拆箱知识整理

    c#装箱和拆箱知识,装箱和拆箱是一个抽象的概念. 1.装箱和拆箱是一个抽象的概念  2.装箱是将值类型转换为引用类型 : 拆箱是将引用类型转换为值类型 利用装箱和拆箱功能,可通过允许值类型的任何值与O ...

  7. php常用函数集锦[备份用的]

    1.判断是否正确的日期格式 /** * 是否正确的日期 * * @access public */ private function _isdate($str,$format="Y-m-d ...

  8. 批处理判断是否存在文件,存在则运行另外一个bat文件

    现在需求如下: 使用bat文件判断是否存在ktr文件,存在则运行pan.bat,执行kettle脚本. 代码如下: @echo off @title 批处理判断文件夹是否存在 cd /d F: rem ...

  9. ORA-00265: instance recovery required, cannot set ARCHIVELOG

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...

  10. JVM学习总结二——垃圾回收算法

    昨天总结了JVM内存分区相关的知识,这次我们将来了解下JVM的另一个核心知识点——垃圾回收算法.这一部分其实并不太难,如果对操作系统的内存处理算法有所了解,那么这部分算法其实只看名字就能明白,两者在原 ...