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

我的递归:

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

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

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

很复杂吧!

如果节点很长的话,这个耗时是很大的。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. Java学习——上转型与下转型对象

    上转型:重写父类方法才调用子类方法,其他仍用父类的,包括被子类隐藏的父类成员变量,而且不能调用子类新增的成员变量和成员方法. 下转型:只能是转上去的才能转下去.下转型类似于该子类直接继承父类. pac ...

  2. NodeJs使用Express框架开发时的快速调试方法

    习惯了php开发,可以直接使用echo或者var_dump()将想要查看的变量结果输出到网页查看,非常的方便.但是使用express开发时,每次修改文件后,都需要使用npm start命令重启服务,然 ...

  3. i++ 和 ++i的字节码指令

    代码 public class Test{ public static void main(String args[]){ int i=0;i=i++; System.out.println(i);} ...

  4. [UE4]RetainerBox,控制UI更新频率,把渲染后的UI当成Texture

    RetainerBox是一个容器,只会影响其容器内的UI,RetainerBox的作用: 一.控制UI更新频率(可能是为有优化性能) 1.在UserWidget中添加Retainer Box容器,并在 ...

  5. 关于把Json数据绑定到select2中

    最近做的一个项目中用到select2,想把Json的数据绑定到select2中,select2默认的能够接受的json格式的数据是以{id:"",text:''}这样的键值对来保存 ...

  6. MySQL 设置密码有效时间

    mysql> alter user 'xuaiqi'@'%' PASSWORD EXPIRE INTERVAL 30 DAY;

  7. 第8章 传输层(7)_TCP连接管理

    7. TCP连接管理 7.1 TCP的连接建立 (1)三次握手 ①三次握手过程 A.第1.2次握手,数据包的SYN均为1,表示用于同步.即第1次客户端发起请求,并将自己的连接参数(如接收窗口大小.MS ...

  8. keras训练和保存

    https://cloud.tencent.com/developer/article/1010815 8.更科学地模型训练与模型保存 filepath = 'model-ep{epoch:03d}- ...

  9. 这些git命令判断提交到哪个分支哪个项目上

    git branch -r fuweikun@pengfei:~/e1_cp/AMSS$ git branch* 8939-E1-2104026-dev git config -l fuweikun@ ...

  10. Delphi XE5中的新增内容

    Delphi XE5中的新增内容 Delphi XE5是所有Delphi开发人员的必须备升级,并且是来自Embarcadero的获奖的.多设备应用开发解决方案的最新版本.使用Delphi XE5的新特 ...