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 ...
随机推荐
- Ubuntu 16.04安装Pycharm2017.1.1
安装pycharm 1.到官网下载安装包. 2.到下载目录下进行解压. 3.运行解压后的文件夹中的bin目录下的pycharm.sh文件. cd pycharm-community-2017.1.1/ ...
- VS2008--VS2013 各种版本官方下载地址
很多刚入门的学生都不知道从哪里下载Visual studio 编译器 , 我特闲的纯手工整理了下 Visual Studio 2005 Professional 官方90天试用版 英文版:http:/ ...
- 敏捷软件开发——第8章 SRP:单一职责原则
第8章 SRP:单一职责原则 一个类应该只有一个发生变化的原因. 8.1 定义职责 在SRP中我们把职责定义为变化的原因.如果你想到多于一个的动机去改变一个类,那么这个类就具有多于一个的职责.同时,我 ...
- ngx_lua_waf
Web应用防护系统Web Application Firewall,简称WAF.针对HTTP/HTTPS的安全策略专门为Web应用提供保护的产品. OpenResty是一个基于 Nginx 与 Lua ...
- 1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- [UE4]传送门:场景切换
- 知识点:Mysql 基本用法之事务
事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 事务实例: create table user( id int primar ...
- Centos7修改系统时区timezone
第一步:查询服务器时间 [root@localhost ~]# timedatectl Local time: Sat 2018-03-31 01:11:46 UTC Universal time: ...
- All entities must be found CRM客户责任人变更报错
问题: 1 修改客户的责任人时,报错. 修改其它字段正常. 2 有的客户可以修改,有的不能修改. 使用 Trace Log 查到的信息: All entities must be found. 原因: ...
- js原生面向对象-仿layui选项卡
喜欢琢磨,给大家分享小编自己封装的仿layui的选项卡. <!DOCTYPE html> <html lang="en"> <head> < ...