问题

给出一个二叉树,将其原地平面化为链表。

例如,给出:

1
   /  \
  2    5
 / \     \
3  4     6

平面化后的树看起来应该是这样:

1
 \
  2
    \
     3
       \
        4
          \
           5
             \
              6

初始思路

观察例子中平面化的过程,不难发现其实就是一个二叉树前序遍历的过程。让我们复习一下二叉树前序遍历的方法,根据wiki条目Tree traversal,伪代码如下:

preorder(node)

  if node == null then return
visit(node)
preorder(node.left)
preorder(node.right)
iterativePreorder(node)
parentStack = empty stack
while not parentStack.isEmpty() or node != null
if node != null then
visit(node)
parentStack.push(node.right)
node = node.left
else
node = parentStack.pop()

这里我们实现基本照搬以上伪代码就行了,唯一一点小改动是增加了一个TreeNode*变量来记录前序遍历过程中上一个节点。这样我们在依前序遍历到第n个节点时,可以通过这个指针将第n-1个节点的right指向当前节点,left置空。选择非递归方案的代码如下:

 class Solution {
public:
void flatten(TreeNode *root)
{
if(!root)
{
return;
} previous_ = nullptr; TreeNode* current = root; while(current || !rightCandidates_.empty())
{
if(!current)
{
current = rightCandidates_.top();
rightCandidates_.pop();
} if(previous_)
{
previous_->right = current;
previous_->left = nullptr;
} previous_ = current; if(current->right)
{
rightCandidates_.push(current->right);
} current = current->left;
}
} private: TreeNode* previous_; std::stack<TreeNode*> rightCandidates_;
};

flatten

Judge Small和Judge Large都顺利通过。

[LeetCode 114] - 将树平面化为链表(Flatten Binary Tree to Linked List)的更多相关文章

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

  2. leetcode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)

    目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树,原地将它展开为链表. 示例: 给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ ...

  3. [Swift]LeetCode114. 二叉树展开为链表 | Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

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

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

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

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

  7. 114 Flatten Binary Tree to Linked List [Python]

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

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

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

随机推荐

  1. linux是一种修行

    或许我当初开始学习linux是因为我在了解嵌入式的时候,查到的资料,说linux是最好的系统,那时可能自己太嫩了,自己就信了,直到最近这几天我才被ubuntu折腾的要死,就是一个环境变量,我折腾怀了我 ...

  2. redis: 6379端口下set值时出现 CLUSTERDOWN The cluster is down

    1.使用./redis-trib.rb check 192.168.242.134:6379检查出现图中的错误 2.在redis安装目录的bin下执行下列命令去cluster meet 另一个端口为7 ...

  3. RMAN-00554: initialization of internal recovery manager package failed RMAN-04005

    [oracle@rac11g1 ~]$ rman target haha/haha@rac11g Recovery Manager: Release 11.2.0.3.0 - Production o ...

  4. NDK GDB 中打印vector , vector<vector <> >

    在android上进行native开发的时候,我们需要用NDK-GDB 对native code进行调试,其中很麻烦的是,我使用的NDK版本是4.0,该版本还不支持用NDK-GDB直接打印vector ...

  5. SKPhysicsBody类

    继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framewo ...

  6. Android TabActivity之感叹

    (一)前言 在以前一篇帖子讲ams的时候,提了一下TabActivity.当时说它比较特殊就没有下文了,今天重发一篇帖子,跟大家探讨一下TabActivity. 做个假定先: 比如我们最外面的Acti ...

  7. 转 [教程] Unity3D中角色的动画脚本的编写(二)

              在上一篇,我们介绍了有关Animation这个类中的部分方法,我后来想了想,这么介绍也不是个办法(其实有些方法我自己也没用过),该介绍点实际的东西了,毕竟我们是要做东西出来的.那好 ...

  8. Win7系统安装MySQL5.5.21图解教程

    转自:http://www.jb51.net/article/37310.htm 这篇文章主要介绍了如何在win7中安装mysql,所以加上了MySQL的下载过程,希望对需要的人有所帮助 大家都知道M ...

  9. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  10. 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

    不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...