当前位置:网站首页>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;
}
}
};
边栏推荐
- MySQL 日期【加号/+】条件筛选问题
- 智能电子界桩自然保护区远程监控解决方案
- Go language go language built-in container
- Do you still have certificates to participate in the open source community?
- C language file reading and writing
- [leetcode] 28. Implement strstr ()
- Socket error Event: 32 Error: 10053. Connection closing...Socket close
- KEGG通路的从属/注释信息如何获取
- 雷达水位计的工作原理及安装维护注意事项
- If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
猜你喜欢

【单细胞高级绘图】07.KEGG富集结果展示

从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试

Increase swap space

ROS_ Rqt toolbox
![[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging](/img/e9/5751dc435cfbbefc22d84fd9ebbaea.png)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging

Struct, enum type and union

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?

增加 swap 空间

智能电子界桩自然保护区远程监控解决方案
随机推荐
CarSim simulation quick start (16) - ADAS sensor objects of CarSim sensor simulation (2)
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
Open source SPL enhances mangodb computing
How to choose a microservice registration center?
MySQL 日期【加号/+】条件筛选问题
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
What is cluster analysis? Categories of cluster analysis methods [easy to understand]
Dataframe first performs grouping operation and then combines output
[advanced mathematics] [5] definite integral and its application
Aircraft PID control (rotor flight control)
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
Redis source code -ziplist
[workplace rules] it workplace rules | poor performance
FanoutExchange交换机代码教程
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
Compilation and operation of program
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
网络爬虫原理解析「建议收藏」
【TensorRT】动态batch进行推理
