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

我的递归:

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

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

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

很复杂吧!

如果节点很长的话,这个耗时是很大的。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. HttpServletRequest接收参数的几种方法

    HttpServletRequest接收参数的几种方法 我们经常用servlet和jsp, 经常用request.getParameter() 来得到数据. request.getParameter( ...

  2. Python twilio发短信实践

    twilio注册地址   注册的时候可能会报错   最好是*** -->注册-->注册完毕后代码运行是不需要***的 https://www.twilio.com/console 需要pi ...

  3. fiddler 手机 https 抓包 以及一些fiddler无法解决的https问题http2、tcp、udp、websocket证书写死在app中无法抓包

    原文: https://blog.csdn.net/wangjun5159/article/details/52202059 fiddler手机抓包原理 fiddler手机抓包的原理与抓pc上的web ...

  4. [UE4]Get Parent,widget获得父容器实例对象

  5. MySQL8.0.12版本密码修改策略问题

    查看密码策略(修改临时密码之后才可查看) show variables like 'validate_password%'; 8之前 validate_password_     8之后validat ...

  6. (转)Linux tcpdump命令详解

    简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中传送的数据包的 ...

  7. 第10课 std::bind和std::function(1)_可调用对象

    1. 几种可调用对象(Callable Objects) (1)普通函数指针或类成员的函数指针 (2)具有operator()成员函数的类对象(仿函数).如c++11中的std::function类模 ...

  8. c#语言集合分析

    集合的赋值: double fenshu = 0; al.Add(fenshu=double.Parse (Console .ReadLine ())); //如果是存数字,将来要比较大小,需要再添加 ...

  9. Session establishment complete on server 2181, sessionid = 0x35fb853eb6f0004

    描述:windows调试 hbase 代码时,出现如下错误:Session establishment complete on server 2181, sessionid = 0x35fb853eb ...

  10. 配置Jsp错误页面

    配置Jsp错误页面一般我们有2种做法: (1)在页面中用指令进行配置,即page指令的errorPage和isErrorPage:可以使用page指令的errorPage来指定错误页!在当前JSP页面 ...