当前位置:网站首页>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;
}
}
};
边栏推荐
- [advanced mathematics] [4] indefinite integral
- [paper reading] unpaired image to image translation using cycle consistent advantageous networks
- Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
- 每条你收藏的资讯背后,都离不开TA
- redis源码 -ziplist
- Network RTK UAV test [easy to understand]
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- 网络RTK无人机上机测试[通俗易懂]
- Kubernetes advanced part learning notes
- During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
猜你喜欢

103. (cesium chapter) cesium honeycomb diagram (square)
![[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

Online XML to JSON tool

Today's sleep quality record 75 points
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born

Embedded development: embedded foundation -- threads and tasks
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP

Why did I choose to become a network engineer after graduating from weak current for 3 months
随机推荐
Compilation and operation of program
[cloud native] use of Nacos taskmanager task management
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
process.env
[advanced mathematics] [4] indefinite integral
Increase swap space
Vulnhub | dc: 6 | [actual combat]
tga文件格式(波形声音文件格式)
文件操作详解
C language file reading and writing
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Struct, enum type and union
Rand1 generates rand9
Has baozi ever played in the multi merchant system?
If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
Docker builds redis cluster
Principle analysis of bootloader
LeetCode通关:哈希表六连,这个还真有点简单
