一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

(二)解题

题目大意:将一个二叉树转换成平树(google来的翻译)

看图就大概知道题目的意思了。

解题思路:flattened tree的顺序就是原树的前序遍历。所以可以定义一颗新树,按照前序遍历来生成右子树。

具体见代码:

/**
 * 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:
    TreeNode* newroot = NULL;//新树的根节点
    TreeNode* p = newroot;//循环中间变量
    void flatten(TreeNode* root) {
        if(root==NULL) return;
        preorder(root);//前序遍历
        //这里有个疑惑:为什么roor=newroot不行?
        root->right = newroot->right;
        root->left = NULL;
    }
    void preorder(TreeNode* root)//中序遍历
    {
        TreeNode* temp = new TreeNode(root->val);
        if(newroot==NULL) newroot=temp;//保存根节点
        else p->right=temp;//构造右子树
        p = temp;
        if(root->left!=NULL) preorder(root->left);
        if(root->right!=NULL) preorder(root->right);
    }
};

【一天一道LeetCode】#114. 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] 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 ...

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

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

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

  7. Java for 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 ...

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

  9. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

  10. LeetCode 114. Flatten Binary Tree to Linked List 动态演示

    把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...

随机推荐

  1. js去掉最后一个字符

    console.log(("0,1,2,3,4,5,".slice(0,-1)))

  2. Go 实现 NumberFormat 函数

    [转] http://www.syyong.com/Go/Go-implements-the-NumberFormat-algorithm.html NumberFormat - 以千位分隔符方式格式 ...

  3. 使用PHP脚本远程部署git项目

    准备工作: 1.coding.net创建私有项目 2.安装了Web服务 Git服务的服务器 服务器端: 1.nginx.php-fpm统一用www用户 www 目录,这个可以通过修改配置文件实现. [ ...

  4. web性能优化之--合理使用http缓存和localStorage做资源缓存

    一.前言 开始先扯点别的: 估计很多前端er的同学应该遇到过:在旧项目中添加新的功能模块.或者修改一些静态文件时候,当代码部署到线上之后,需求方验收OK,此时你送了一口气,当你准备开始得意于自己的ma ...

  5. YARN整理

    YARN整理 1.YARN的介绍 是一个资源管理.任务调度的框架,主要包含三大模块: ResourceManager(RM):负责所有资源的监控.分配和管理 ApplicationMaster(AM) ...

  6. day07 Cookie 和 Session(非常重要)

    day07 Cookie 和 Session 1. 会话技术 2. cookie 的方法和 cookie 案例-显示用户上次访问网站的时间 3. cookie 的细节 - 删除 cookie 4. S ...

  7. 0428css样式

    CSS样式表|-引入的三种方式|--内联样式|----标签内部(空格style)|--内嵌样式|----<head></head>标签内部(<style></ ...

  8. log4j日志记录级别是如何工作?

    级别p的级别使用q,在记录日志请求时,如果p>=q启用.这条规则是log4j的核心.它假设级别是有序的.对于标准级别它们关系如下:ALL < DEBUG < INFO < WA ...

  9. GitHub Android Librarys Top 100 简介

    GitHub Android Librarys Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据GitHub搜索J ...

  10. Android简易实战教程--第四十五话《几种对话框》

    Android中提供了各种原生的对话框,在使用简单的功能的时候,还不比考虑自定义,使用原生的也能完成功能.本篇简单小案例就介绍三种对话框. 还是直接上代码吧: 布局中三个点击事件的按钮: <Li ...