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

也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
while(root){
if(root->left){
TreeNode * leftBegin = root->left;
root->left = NULL;
TreeNode * leftEnd = leftBegin;
while(leftEnd->right)
leftEnd = leftEnd->right;
TreeNode * tmpRight = root->right;
root->right = leftBegin;
leftEnd->right = tmpRight;
}
root = root->right;
}
}
};

用java写了一遍,方法与上面基本相同,代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
while(root != null){
if(root.left != null){
TreeNode tmp = root.right;
root.right = root.left;
TreeNode tmpRight = root.right;
while(tmpRight.right != null)
tmpRight = tmpRight.right;
tmpRight.right = tmp;
root.left = null;
}
root = root.right;
}
}
}

LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)的更多相关文章

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

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

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

  3. leetcode dfs Flatten Binary Tree to Linked List

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

  4. [LeetCode] 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 ...

  5. [LeetCode] 114. 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 ...

  6. [Leetcode][JAVA] 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   ...

  7. 【leetcode】Flatten Binary Tree to Linked List (middle)

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

  8. leetcode 114 Flatten Binary Tree to Linked List ----- java

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

  9. [leetcode]114. 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 ...

  10. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

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

随机推荐

  1. 爬虫,如何防止被ban之策略大集合

    话说在尝试设置download_delay小于1,并且无任何其他防止被ban的策略之后,我终于成功的被ban了. 关于scrapy的使用可参见之前文章: http://blog.csdn.net/u0 ...

  2. 一个用 C# 实现操作 XML 文件的公共类代码

    using System; using System.IO; using System.Data; using System.Xml; using System.Xml.XPath; namespac ...

  3. ajax 请求后台数据返回异常 及 提示404方法名不存在

    1.正常使用 Ajax 调取后台数据时,提示方法名不存在,Ajax前端正常,方法类bean注入正常,方法注解正常.但参数解析时出现异常. @RequestMapping(value="/ge ...

  4. Linux:文件

    Linux:文件 文件属性 用户分为三种:文件拥有者.群组以及其它人,对不同的用户有不同的文件权限. 使用 ls 查看一个文件时,会显示一个文件的信息,例如 drwxr-xr-x. 3 root ro ...

  5. Android:日常学习笔记(8)———探究UI开发(5)

    Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...

  6. 本地yum源建立

    一.openstack(ocata)本地yum源的建立: 1.配置yum缓存: vi /etc/yum.conf 把yum.conf配置改为: [main] cachedir=/var/cache/y ...

  7. 每天一个Linux命令(47)route命令

        Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table).     (1)用法:     用法:  route ...

  8. Windows10在待机状态时会卡屏的解决方案

    问题:Windows10在待机时,会出现卡屏(鼠标.键盘都无法操作,只能重启电脑),区别于平时我们看得比较多的花屏.蓝屏.黑屏. 原因:经过一段时间的待机,Windows10会进入到降电节能模式,由于 ...

  9. GCC编译器入门[转载]

    GCC编译器(GNU C Compiler)是GNU组织的一款开源 编译器,它是Linux环境下的默认C语言编译器.它处理能够高效的编译C语言以外,还可以编译其他语言.并且,现在的GCC已经不光包括编 ...

  10. python中threading多线程

    python中有两个处理多线程的模块thread和threading.其中thread提供了多线程底层支持的模块,以低级原始的发那个是来处理和控制线程,使用起来较为复杂:而threading基于thr ...