LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距!
我的递归:
先把左侧扁平化,再把右侧扁平化。
然后找到左侧最后一个节点,把右侧移动过去。
然后把左侧整体移到右侧,左侧置为空。
很复杂吧!
如果节点很长的话,这个耗时是很大的。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:别人的递归!的更多相关文章
- [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 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 【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 ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
- [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 ...
- [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 ...
- [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 ...
- 【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 ...
- 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 ...
- [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 ...
随机推荐
- HDOJ 2002 计算球体积
#include<cstdio> #define PI 3.1415927 int main() { double r; while (scanf_s("%lf", & ...
- python实现简单的登陆认证(含简单的文件操作)
需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(本 ...
- Android知识点textview加横线的属性
textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线 textView.getPaint().setAntiAlias(tr ...
- echarts折现图配置
js引用和div容器 <div id="container" style="height: 100%"></div> <scrip ...
- SSH框架总结(环境搭建+框架分析+实例源码下载)
一.SSH框架简介 SSH是struts+spring+hibernate集成的web应用程序开源框架. Struts:用来控制的,核心控制器是Controller. Spring:对Struts和H ...
- /storage/xx-xx/, /sdcard, /mnt/sdcard 三者的区别
本文针对Android 7.1 /sdcard是/mnt/sdcard的符号链,指向/storage/self/primary, /mnt/sdcard,也是符号链,指向/storage/self/p ...
- Delphi 解决Utf8ToAnsi和Utf8DeCode转换编码为空的问题
//delphi DecodeUtf8Str解决系统自带UTF8解码缺陷 function DecodeUtf8Str(const S: UTF8String): WideString; var le ...
- CS229 6.9 Neurons Networks softmax regression
SoftMax回归模型,是logistic回归在多分类问题的推广,即现在logistic回归数据中的标签y不止有0-1两个值,而是可以取k个值,softmax回归对诸如MNIST手写识别库等分类很有用 ...
- JQ替换标签与内容
JQ: $('#Status').replaceWith(function () { return $("<select ><option value='0'>未认证 ...
- centos磁盘挂载|centos虚拟机硬盘不够怎么办?|centos虚拟机硬盘的扩展
Centos6磁盘挂载 添加一块磁盘 分区,格式化,挂载新磁盘 磁盘挂载 df -lh fdisk -l fdisk /dev/sdb 这个命令执行后依次输 n p 回车 回车 w fdisk -l ...