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
Hints:

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

void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return;
if(root->left == NULL && root->right == NULL) return;
flatten(root->left);
flatten(root->right); TreeNode* tmpright = root->right;
root->right = root->left;
root->left = NULL;
TreeNode* tmp = root;
while(tmp->right)
tmp = tmp->right;
tmp->right = tmpright;
return;
}

leetcode_question_114 Flatten Binary Tree to Linked List的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【转】linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解----不错

    原文网址:http://blog.csdn.net/liaoxinmeng/article/details/5004743 首先: 1.终端和控制台都不是个人电脑的概念,而是多人共用的小型中型大型计算 ...

  2. 个性化定制——物流app

    众所周知,在互联网不断迈进的大环境下,各行各业都不免在这大潮下纷纷卷入.人们早已不再满足于传统行业,即便是所谓的新兴行业所带来的体验,他们更多的希望能够在便捷的基础上获取更加个性化的服务,个性化服务在 ...

  3. ibatis学习之道:ibatis的<[CDATA]>dynamic属性跟#$的应用

    ibatis的<![CDATA]>,dynamic属性和#,$的应用 <![CDATA[   ]]>的正确使用 ibatis作为一种半自动化的OR Mapping工具,其灵活性 ...

  4. centerOS安装rkhunter

    rkhunter是专业检测系统是否感染rootkit的一个工具: rkhunter-1.4.2.tar.gz 解压后直接安装: #./installer.sh --layout defualt --i ...

  5. Hibernate中Session的几个方法的简单说明

    Hibernate对普通JDBC的操作进行了轻量级的封装,使得开发人员可以利用Session对象以面向对象的思想来实现对关系数据库的操作. 一般通过Configuration对象读取相应的配置文件来创 ...

  6. html5+css3中的background: -moz-linear-gradient 用法 (转载)

    转载至-->http://www.cnblogs.com/smile-ls/archive/2013/06/03/3115599.html 在CSS中background: -moz-linea ...

  7. python StringIO标准库基础学习

    #标准库:StringIO提供类文件API文本缓冲区#作用:可以处理内存中的文本,有2种不同的实现:cStringIP版本用c编写提高速度,StringIO用python来提供可移植性,与其他字符串连 ...

  8. oracle修改字符集后数据库不能启动

    最近在做修改字符集的实验,悲剧的是修改后重启,数据库启动不了. SQL> alter system set nls_language='AMERICAN'   scope = spfile; S ...

  9. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  10. HDU 4760 Good FireWall 完好Trie题解

    本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...