Flatten Binary Tree to Linked List

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

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

前序遍历,按访问序往右加就行了。

需要注意的是,加入单链之后要删掉原先的子女链接。

/**
* Definition for binary tree
* 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; TreeNode* newtail = new TreeNode(-);
stack<TreeNode*> stk;
stk.push(root);
while(!stk.empty())
{
TreeNode* top = stk.top();
newtail->right = top;
newtail = newtail->right;
stk.pop(); if(top->right)
stk.push(top->right);
if(top->left)
stk.push(top->left); top->left = NULL;
top->right = NULL;
}
}
};

【LeetCode】114. Flatten Binary Tree to Linked List的更多相关文章

  1. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

  2. 【一天一道LeetCode】#114. Flatten Binary Tree to Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. LeetCode OJ 114. Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

  7. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

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

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

  9. 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. mysql存储过程导入表

    运用存储过程,把用户表一数据导入用户表二 DELIMITER @@ CREATE PROCEDURE imp_to_user2() BEGIN – 声明一个标志done, 用来判断游标是否遍历完成 D ...

  2. mybatis源码分析(2)-----SqlSession创建

    1. 在创建好sqlSessionFactory之后,接着就要配置sqlSession的创建. <bean id="simpleTempalte" class="o ...

  3. Cascode MOSFET increases boost regulator's input- and output-voltage ranges

    Targeting use in portable-system applications that require raising a battery's voltage to a higher l ...

  4. DELPHI PROTOBUF免费的开源支持库fundamentals5

    DELPHI PROTOBUF免费的开源支持库fundamentals5 1.源码URL: https://github.com/fundamentalslib/fundamentals5 2.编译P ...

  5. 使用富文本OHAttributedLabel

    OHAttributedLabel 富文本标签 https://github.com/AliSoftware/OHAttributedLabel 以下是我渲染出来的效果 OHAttributedLab ...

  6. 关于Mantis变更日志(Changelog)和路线图(Roadmap)的说明

    变更日志(Changelog):是已经修改好了问题的日志,需要给项目添加版本号,并且在添加/解决问题时都指定了相应的版本号,才会显示. 路线图(Roadmap):是计划在某个版本修改某些问题的日志,需 ...

  7. 角摩网发布在线制作Epub、Mobi格式的电子书

    原来cn的域名没有及时续约被人用了,现在用www.joymo.cc开始新的电子书制作之路. 目前支持Epub和Mobi格式,会陆续加入PDF和APK的电子书.

  8. 斯坦福大学卷积神经网络教程UFLDL Tutorial - Convolutional Neural Network

    Convolutional Neural Network Overview A Convolutional Neural Network (CNN) is comprised of one or mo ...

  9. Informatica 常用组件Source Qualifier之二 默认查询

    2 默认查询 对于关系源,PowerCenter Server 将在运行会话时为每个源限定符转换生成查询.对于每个在映射中使用的源列,默认查询均为 SELECT 语句.也就是说,PowerCenter ...

  10. 如何在SharePoint的列表中使用通配符来filter出ListItem?

    一个朋友问我这样一个问题, 他想要快速从SharePoint的文档库中filter出来名字中先带有一个Q, 接着一些其他的字符, 后面再跟着有一个数字20这样的文件.   第一个想法就是修改Share ...