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.

Solution:

     void flatten(TreeNode *root) {
stack<TreeNode *> nodes;
if(root == NULL)
return;
nodes.push(root);
TreeNode * pre = root;
while(nodes.size() != ){
TreeNode * node = nodes.top();
nodes.pop();
if(node->right != NULL)
nodes.push( node->right);
if(node->left != NULL)
nodes.push( node->left);
pre->left = NULL;
if(node != root){
pre->right = node;
pre = pre->right;
} }
}

Flatten Binary Tree to Linked List [LeetCode]的更多相关文章

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

  2. Flatten Binary Tree to Linked List ——LeetCode

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

  3. Flatten Binary Tree to Linked List leetcode java

    题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...

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

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

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

  6. Leetcode: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. [LeetCode]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 e ...

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

  9. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

随机推荐

  1. centos7 卸载openJDK 安装jdk7

    [root@cms02 root]# rpm -qa | grep jdk java--openjdk-headless-1.7.0.75-2.5.4.2.el7_0.x86_64 java--ope ...

  2. 【转】struts1.2的action参数配置

    转载地址:http://chenfeng0104.iteye.com/blog/796870 <struts-config>     <form-beans>         ...

  3. input标签file的value属性IE兼容性问题

    在IE中input标签file的value属性是只读的,不能通过js来改变,如下代码在IE中就是无效的: var input = document.getElementById('file'); in ...

  4. mybatis框架下解决数据库中表的列的字段名和实体类属性不相同的问题

    导包.... 实体类中的属性,getter,setter,tostring,构造等方法就不写了 private int id; private String orderNo; private floa ...

  5. JS调用JCEF方法

    坐下写这篇文章的时候,内心还是有一点点小激动的,折腾了一个多星期,踩了一个又一个的坑,终于找到一条可以走通的路,内心的喜悦相信经历过的人都会明白~~~~~今儿个老百姓啊,真呀个真高兴啊,哈哈,好了,废 ...

  6. laravel框架总结(十) -- 返回值

    以前用CI框架对于返回值没有过多关注,但是发现使用laravel框架的时候出现了一些小问题,特意实践总结了一些常用情形,希望对大家有所帮助   先理解几个概念: 1>StdClass 对象=&g ...

  7. 从零开始学习Android(二)从架构开始说起

    我们刚开始学新东西的时候,往往希望能从一个实例进行入手学习.接下来的系列连载文章也主要是围绕这个实例进行.这个实例原形是从电子书<Android应用开发详解>得到的,我们在这里对其进行详细 ...

  8. Expression2Sql的一些语法更新

    前言 前一阵子给大家介绍了一个可以将Expression表达式树解析成Transact-SQL的项目Expression2Sql. 之后得到了广大读者的一些好评,也使得博主更有动力继续更新下去,然后一 ...

  9. 存储过程里面使用in变量列表异常的处理

    在写一个存储过程的时候,由于需要用到类似:select id,name from tablename where id in(id1,id2,id3...)的查询语句,同时括号里面的变量是拼接得到的, ...

  10. '"千"第一周学习情况记录

    一周过去了,今天将我这一周的学习内容和主要感想记录与此和大家共同分享,一起进步.我将自己的学习计划命名为"千",因为我喜欢这个字,希望能用此来鼓舞自己不断前进.时间总是很快的,这一 ...