题目:

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. jquery 获取元素坐标

    不错哦! 原文地址:http://jianzhong5137.blog.163.com/blog/static/9829049201182295833503/ 绝对X,Y坐标,可以用offset()方 ...

  2. CentOS 下如何查看并清理系统内存空间

    有时候在服务器上打开了很多会占用内存的程序但关闭这些程序后,发现内存空间还是和没有关闭应用程序时的占用一样,以致使其它应用程序打开时内存不够或很卡,那么此时就想清理掉以前的程序打开时所占用的内存.而大 ...

  3. php的register_shutdown_function函数详解

    function shutdown() { $last_error = error_get_last(); if ($last_error) { error_log(print_r($last_err ...

  4. Jquery note

    the purpose: write less. do more   写得少,做更多 jquery 基本选择器, $("p ,div ")匹配所有的P元素和DIV元素 , $(&q ...

  5. javascript中方法调用与方括号[]

    看jquery时遇到一行: $(this)["removeClass"]("selected"); 这一行等同于下面的一行: $(this).removeCla ...

  6. SQL Server 一些关键字详解(二)

    1.LEFT JOIN 容易让人误解的地方 背景:因为在网上搜了下 LEFT JOIN 和 OUTER APPLY 的区别,时发现,有的网友解释为: 1) A   left  join  B  的连接 ...

  7. oracle分区表(整理)

    Oracle 表分区 早在8.0.5版本中,Oracle就将范围分区技术引入,现在分区功能已经越来越强大,包括支持扩展分区功能.Interval分区.外键分区.模拟列分区.以及分区建议器等.那么,分区 ...

  8. 九度oj 1530 最长不重复子串

    原题链接:http://ac.jobdu.com/problem.php?pid=1530 字符串简单题,看似O(n^2)的复杂度10000的数据量会tle,其实最长不重复子串不超过26个嘛... 如 ...

  9. centos6.5适用的国内yum源:网易、搜狐

    设置方法如下: 1,进入yum源配置目录cd /etc/yum.repos.d 2,备份系统自带的yum源mv CentOS-Base.repo CentOS-Base.repo.bak 下载163网 ...

  10. [转]编译错误: /bin/sh: 1: pushd: not found的问题

    [转]编译错误: /bin/sh: 1: pushd: not found的问题 http://blog.csdn.net/ojinxi/article/details/12186839 ubuntu ...