总是在看完别人的代码之后,才发现自己的差距!

我的递归:

先把左侧扁平化,再把右侧扁平化。

然后找到左侧最后一个节点,把右侧移动过去。

然后把左侧整体移到右侧,左侧置为空。

很复杂吧!

如果节点很长的话,这个耗时是很大的。O(n^2) ?差不多了!

菜逼啊!时间估计都错了!!!

时间是多少呢?

while 最左侧的数,会不断被遍历!是这样的。大概会被遍历o(n)次

所以还是O(n^2)?

反正是复杂了。

void flatten(struct TreeNode* root) {
if(root == NULL) return; flatten(root->left); flatten(root->right); if(root->left)
{
struct TreeNode *p = root->left;
while(p -> right)
{
p = p->right;
}
p->right = root->right;
root->right = root->left;
root->left = NULL;
}
return root;
}

看看人家多么行云流水的操作!代码简洁,效率高!没有重复操作!厉害啊!

struct TreeNode *pre = NULL;

void convert(struct TreeNode* root)
{
if(root == NULL) return; convert(root->right);
convert(root->left); root->right = pre;
root->left = NULL;
pre = root;
} void flatten(struct TreeNode *root)
{
pre = NULL;
convert(root);
}

什么思路?

先把右侧变成一个链表,记录下表头。

再把左侧变成链表,右侧的表头作为尾部。

右子树变成左子树的最右儿子的右儿子。

巧妙啊。。。

另外,要注意全局变量每次使用前都要重新初始化。所以外面套了一层。

LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!的更多相关文章

  1. [LeetCode 题解]: 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 ...

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

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

  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. leetcode dfs Flatten Binary Tree to Linked List

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

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

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

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

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

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

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

随机推荐

  1. 如何进行CodeReview

    一.代码规范的要点 代码规范主要分为风格规范与设计规范两大类: 1.代码风格规范 主要是文字上的规定,看似表面文章,实际上非常重要. 具体有如下几个方面: (1)缩进 (2)行宽 (3)断行/空白行 ...

  2. base_基础

    目录 A B C D E F G H I J K L M N S: Sqlite: 1;orhanobut/hawk; A: Adapter: 图片处理 Android中自定义布局中加载图片Bitma ...

  3. 智行火车票免费加速到VIP最高速抢票(不用朋友积攒或者购买加速包)

    更新: 2018.11.07, 昨天我买火车票,已经不行了,这个bug已经没有了,被修复了, 望大家知悉!!! 智行火车票免费加速到VIP最高速抢票(不用朋友积攒或者购买加速包) 1)下过单后选择抢到 ...

  4. 关于虚拟机下centOS版linux系统ifconfig只显示inet6ip,不显示inet4ip的问题

    在linux命令窗口输入ifconfig会显示如下 [root@localhost Desktop]# ifconfig eth0   Link encap:Ethernet  HWaddr 00:0 ...

  5. css段落(后盾)

  6. SerializeHelper

    using System; using System.Collections.Generic; using System.Configuration; using System.IO; using S ...

  7. mybatis的插件,挺好支持下

    利用 Mybatis-generator自动生成代码http://www.cnblogs.com/yjmyzz/p/4210554.html Mybatis 通用 Mapper3 https://gi ...

  8. 你创建的OpenStack高性能虚拟机能实现“零损耗”么?

    使用默认参数创建的虚拟机,虚拟机的VCPU在物理CPU不同核心之间动态调度,另外,由于Linux还可能会将软中断,内存交换等进程调度到虚拟机正在使用的物理核心上,这些因素导致这些虚拟机相对于物理机的计 ...

  9. implementation compile的区别

    implementation不可以依赖传递:依赖对app Module 是不可见的 compile可以依赖传递:依赖对app Module 是可见的 AndroidStudio升级到3.0以上后,第一 ...

  10. 流(Stream)与文件流(FileStream)

    //通过流的方式添加 StreamWriter writer = new StreamWriter(@"C:\A\ca.txt", true, Encoding.Default); ...