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

    $ mklink /D 参数:创建目录类型链接

  2. JS获取form表单所有属性值

    // 得到一个表单里的全部信息function getFormQueryString() { var frmID=document.forms[0]; var i,queryString=" ...

  3. HTTP头的Expires与Cache-control

    HTTP头的Expires与Cache-control 1.概念 Cache-control用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache) 数据 ...

  4. HsqlDB Demo

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  5. SVN服务器的搭建和使用

    VisualSVN Server和TortoiseSVN的下载,安装,汉化:SVN服务器搭建和使用(一) 如何使用VisualSVN Server建立版本库,以及TortoiseSVN的使用:SVN服 ...

  6. TCP/IP之大明内阁---协议的制定

    个人感言:真正的知识是深入浅出的,码农翻身" 公共号将苦涩难懂的计算机知识,用形象有趣的生活中实例呈现给我们,让我们更好地理解.感谢"码农翻身" 公共号,感谢你们的成果, ...

  7. dedecms后台验证码显示不正常的四种处理办法

    验证码不正确解决方法 分为两类解决方法 第一类:取消掉验证码,直接登录 第二类:修复验证码,回复验证码功能 四种常见的处理办法如下: 第一种:取消掉验证码具体方法如下 实现的方法一共分为两步来进行: ...

  8. swift 键盘属性与事件

    1.键盘的类型 textField1.keyboardType = UIKeyboardType.default //系统默认的虚拟键盘 textField1.keyboardType = UIKey ...

  9. Vue2父子组件通信探究

    父组件: <template> <div id="secondcomponent"> <input type="" v-model ...

  10. IAR调节字体大小

    在主面板上点击tools->Options,然后点开Editor,选择下面的Colors and Fonts选项,最后选右上方的Font,选择要设置的字体就OK了.