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.

Subscribe to see which companies asked this question

解答

先序遍历同时把节点都堆到左边,因为先序先处理左子树,所以这样操作只是更改叶节点的left而不会对遍历有影响,最后把所有left赋值给right就可以了。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 struct TreeNode *pre_node;
 void DFS(struct TreeNode *root){
     if(NULL == root){
        return;
    }
    if(pre_node != root){
        pre_node->left = root;
        pre_node = root;
    }
    DFS(root->left);
    DFS(root->right);
 }
void flatten(struct TreeNode* root) {
    pre_node = root;
    DFS(root);
    while(root != NULL){
        root->right = root->left;
        root->left = NULL;
        root = root->right;
    }
}

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

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

  2. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

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

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

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

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

  5. LeetCode OJ: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 ...

  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 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

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

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

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

随机推荐

  1. Codeforces Round #232 (Div. 2) D. On Sum of Fractions

    D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...

  2. Struts2:效验器——注解

    效验器三类: 编程式——Java代码 声明式——xml 注释法——@ 注解验证可以修饰属性的getter方法,也可以修饰执行方法Action中校验失败时,返回input逻辑视图 struts.xml ...

  3. Servlet基础

    今天在学习Servlet的时候遇到了一个问题:大概是这样java.lang.ClassNotFoundException: HelloServlet at org.apache.catalina.lo ...

  4. Linux 网络基本配置

    一.Linux网络配置文件 1.  /etc/sysconfig/network-scripts/ifcfg-eth0 文件 在Red Hat系统中,系统网络设备的配置文件保存在/etc/syscon ...

  5. 新手上路,配置阿里云CentOS服务器LAMP

    最近出了个9.9包月的阿里云一直想弄就买了个半年的,脑子一热选了个CentOS系统的然后就是无尽的代码...... 话不多说直接上步骤:  在实例中选择"连接管理终端",输入一些应 ...

  6. 浅尝辄止——使用ActiveX装载WPF控件

    1 引言 使用VC编写的容器类编辑器,很多都可以挂接ActiveX控件,因为基于COM的ActiveX控件不仅封装性不错,还可以显示一些不错的界面图元. 但是随着技术不断的进步,已被抛弃的Active ...

  7. java 工具类

    首先把构造函数设置为私有.然后方法都改为静态.

  8. MVC中Control和View之间数据传递的方式

    1:ViewBag和ViewData 具体区别不做讨论,本处只演示ViewData的具体示例: Controler代码:ViewData["Employee"] = emp; Vi ...

  9. mysql 忘记root 密码的解决方法

    LINUX 1.切换root 用户 2.停止mysqld 服务 /etc/inid.d mysqld stop 3.跳过验证登录 mysqld_safe --skip-grant-tables &am ...

  10. iOS中CocoaPads的安装与配置(总结)

    来说一下安装CocoaPots的顺序: Xcode->homebrew->RVM->Ruby->CocoaPats; 这篇文章之讲解如何安装到Ruby的部分,关于安装好Ruby ...