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

思想: 如 Hints.

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* getPreNode(TreeNode *root) {
if(root == NULL) return NULL;
while(root->right) root = root->right;
return root;
}
class Solution {
public:
void flatten(TreeNode *root) {
if(root == NULL) return;
TreeNode *pre = getPreNode(root->left);
if(pre) {
pre->right = root->right;
root->right = root->left;
root->left = NULL;
}
flatten(root->right);
}
};

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

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

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

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

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

  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. jsp与php混用的漏洞

    接手一个项目是jsp写的,用起来感觉开发是在太麻烦了.于是新功能就用php写的,jsp.php两者之间相互跳转, 突然一天发现在tomcat的web站下打开php竟然显示了php源码,在php站下看j ...

  2. 我原来忽略的web开发点

    打开一个网页,看到的东西的背后还有看不见的东西,程序员通常在一个页面影藏了许多标签,这个页面可以用来在许多地方使用,因为模板相同,只是有点地方不一样.还有类似于新浪微博的页面使用了很多花样,消息推送( ...

  3. HBase with MapReduce (Read and Write)

    上面一篇文章仅仅是介绍如何通过mapReduce来对HBase进行读的过程,下面将要介绍的是利用mapreduce进行读写的过程,前面我们已经知道map实际上是读过程,reduce是写的过程,然而ma ...

  4. 用idea写servlet文件

    1:File->Project Structure 加号处添加tomcat api(在lib目录下) 2:写servlet文件 src->new servlet写好类名和包名 3:配置we ...

  5. php 无法连接mysql

    sql_connect,sqli_connect, 或new sqli() 无法建立mysql连接 1. php.ini 中 ; extension=php_mysql.dll 和 ; extensi ...

  6. OWL,以及XML,RDF

    Ontology来源于哲学词汇:存在论(也有翻译成本体论).RDF是一种不错的本体描述方式,我们可以定义根据对现实世界的理解针对某个领域定义词汇来描述这个领域的知识.但RDF与RDF不能定义同义词.反 ...

  7. GridView控件中加自动排列序号

    GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField  HeaderText="序号">    < ...

  8. linux命令:find

    1.命令介绍: find用来在整个系统指定的路径下搜索文件,功能强大,但是遍历整个系统时很耗时间. 2.命令格式: find 路径 [选项] [print -exec -ok...] 3.命令参数: ...

  9. dij单源最短路纯模板

    #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...

  10. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...