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 ...
随机推荐
- Jmeter(二十四)Jmeter-Question之“加密请求参数”
日常接口测试碰到参数加密的情况不在少数,当然与之相对的也有解密.直接记录实例: 排除各家用的不一样的加密方式,用的最多的还是MD5加密(16,32).Jmeter3.2版本已经有解决方案 1.${__ ...
- 实现textview竖排文字效果
文字效果
- CentOS7 设置集群时间同步
1. 安装ntp时间同步工具 yum -y install ntp ntpdate #安装ntpdate时间同步工具 ntpdate cn.pool.ntp.org #设置时间同步 hwclock - ...
- HBase脚本命令
1. 脚本使用小结1.开启集群 start-hbase.sh 2.关闭集群 stop-hbase.sh 3.开启/关闭[所有]的regionserver.zookeeper hbase-daemons ...
- 线程守护进程与GIL
为何要用多线程 多线程指的是,在一个进程中开启多个线程,简单的讲:如果多个任务共用一块地址空间,那么必须在一个进程内开启多个线程.详细的讲分为4点: 1. 多线程共享一个进程的地址空间 2. 线程比进 ...
- Flume+HBase+Kafka集成与开发
先把flume1.7的源码包下载 http://archive.apache.org/dist/flume/1.7.0/ 下载解压后 我们通过IDEA这个软件来打开这个工程 点击ok后我们选择打开一个 ...
- build.gradle 中compileSdkVersion,minSdkVersion,targetSdkVersion,buildToolsVersion的意思
compileSdkVersion: 编译版本:compileSdkVersion告诉gradle使用哪个版本AndroidSDK编译你的应用: minSdkVersion: 最低SDK版本:他代表的 ...
- day3(第一周)周末作业
1.创建字符串变量的三种写法及其区别# 代码:单引号 ''# 双引号 ""# 多引号 ''' '''# 区别:单引号和双引号没有任何区别,一般用于单行字符:多行字符用多引号.## ...
- 爬虫概念 requests模块
requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...
- canvas实现刮刮乐
效果展示 源码下载