LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目
给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
1
/ \
2 5
/ \ \
3 4 6
将其展开为:
1
\
2
\
3
\
4
\
5
\
6
解析
- 通过递归实现;可以用先序遍历,然后串成链表
- 主要思想就是:先递归对右子树进行链表化并记录,然后将root->right指向 左子树进行链表化后的头结点,然后一直向右遍历子树,连接上之前的右子树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* treetolist(TreeNode* root)
{
if(!root)
return NULL;
TreeNode* right=treetolist(root->right); //记录右子树
root->right=treetolist(root->left);
root->left=NULL;
TreeNode* cur=root;
while(cur->right)
{
cur=cur->right;
}
cur->right=right;
return root;
}
void flatten(TreeNode* root) {
treetolist(root);
}
};
- 理解上面代码过后就容易理解:LeetCode | Flatten Binary Tree to Linked List(二叉树转化成链表)
LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)的更多相关文章
- [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 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 ...
- [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- Java for 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 ...
- 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 ...
- 114 Flatten Binary Tree to Linked List 二叉树转换链表
给定一个二叉树,使用原地算法将它 “压扁” 成链表.示例:给出: 1 / \ 2 5 / \ \ 3 4 6压扁后变成如下: ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
随机推荐
- Django(request和response)
原文链接: https://blog.csdn.net/weixin_31449201/article/details/81043326 Django中的请求与响应 一.请求request djang ...
- android 多线程的实现方式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha. 1,通过创建线程池,来创建多个线程. 2,通过异步任务,来创建线程池,从而创建多线程. ...
- WordPress 建站教程:新手搭建 WordPress个人博客图文教程(完全版)
前言 WordPress 作为动态博客的代表,至今已经有十几年历史,而且一直在更新发展中,功能强大,插件和主题丰富,WordPress搭建使用也很方便.作为个人站长和博主,很多都是从 WordPres ...
- 20172319 实验四 《Android程序设计》实验报告
20172319 2018.05.17-30 实验四<Android程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1723班 学生姓名:唐才铭 学生学号:2 ...
- Intel P6以来核心架构及对应型号、芯片组一览表
转载或拿走使用请注明出处,谢谢! 注1:5系列以前的芯片组部分可以支持多代处理器(如部分945可以支持65nm.45nm的处理器),5系列开始此现象较少见. 注2:插座兼容性①Socket370接口处 ...
- Bitbox : a small open, DIY 32 bit VGA console
Bitbox : a small open, DIY 32 bit VGA console Hi all, I've been developing a simple DIY console and ...
- MDK5 and STM32Cube
D:\Workspace\........\RTE\Device>STM32CubeMX.exe -s project.script -tpl_path C:\Keil5\ARM\Pack\Ke ...
- STM32F4, USB HS with ULPI and Suspend/Wakeup
Hi guys,I am in need of your help, unfortunately STs documentation is lacking some information here. ...
- intel32指令中文版
http://scc.qibebt.cas.cn/docs/optimization/VTune(TM)%20User's%20Guide/mergedProjects/analyzer_ec/mer ...
- [置顶] Linux下发布QT程序
Linux下发布QT程序 概述 无论在windows下还是在linux下,可执行程序的运行都依赖于相关的运行库,我们需要将依赖的库找到放到特定的位置,让可执行文件能够找到.在不知道可执行文件依赖哪些库 ...