当前位置:网站首页>leetcode-114:二叉树展开为链表
leetcode-114:二叉树展开为链表
2022-07-25 20:36:00 【菊头蝙蝠】
题目
题目连接
给你二叉树的根结点 root ,请你将它展开为一个单链表:
展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
展开后的单链表应该与二叉树 先序遍历 顺序相同。
示例 1:

输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [0]
输出:[0]
解题
方法一:
class Solution {
public:
void flatten(TreeNode* root) {
while(root){
TreeNode* p=root->left;
if(p){
while(p->right) p=p->right;
p->right=root->right;
root->right=root->left;
root->left=nullptr;
}
root=root->right;
}
}
};
边栏推荐
- 第六章 修改规范(SPEC)类
- The database empties the table data and makes the primary key start from 1
- Compilation and operation of program
- Is QQ 32-bit or 64 bit software (where to see whether the computer is 32-bit or 64 bit)
- Automated testing ----- selenium (I)
- 结构体,枚举类型与联合体
- FanoutExchange交换机代码教程
- 103. (cesium chapter) cesium honeycomb diagram (square)
- Docker 搭建 Redis Cluster集群
- JMeter - interface test
猜你喜欢

Leetcode customs clearance: hash table six, this is really a little simple

Go language go language built-in container
![[leetcode] 28. Implement strstr ()](/img/87/0af2da458e53d31012d6535cc132bb.png)
[leetcode] 28. Implement strstr ()

Kubernetes advanced part learning notes
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

Today's sleep quality record 75 points
![[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world](/img/41/76687ea13e1722654b235f2cfa66ce.png)
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world

网络协议:TCP Part2
![[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger](/img/14/f2b68dbe4e6a9b8d89ed9ff38f5e11.png)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
随机推荐
每条你收藏的资讯背后,都离不开TA
JS scope and scope chain
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
Implementation of simple registration and login
[workplace rules] it workplace rules | poor performance
[advanced mathematics] [6] differential calculus of multivariate functions
网络爬虫原理解析「建议收藏」
103. (cesium chapter) cesium honeycomb diagram (square)
How to obtain the subordinate / annotation information of KEGG channel
Fanoutexchange switch code tutorial
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
Jmeter——接口测试
Stock software development
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance
How much memory does bitmap occupy in the development of IM instant messaging?
Vulnhub | dc: 6 | [actual combat]
Leetcode customs clearance: hash table six, this is really a little simple
Principle analysis of bootloader
