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成 ...
随机推荐
- 10BASE
10BASE-T,10BASE-5,10BASE-2,以太网的技术标准,10Base-2.10Base-5.10Base-T都是以太网的技术标准,传输速率为10Mbps. 10Base-2技术以细 ...
- Linux驱动之串口(UART)
<uart驱动程序概述> 在嵌入式Linux系统中,串口被看成终端设备,终端设备(tty)的驱动程序分为3部分: tty_core tty_disicipline tty_driver ...
- android studio 使用总结
网站1:http://stormzhang.com/posts.html 网站2:http://blog.csdn.net/hyr83960944/article/details/38388429
- Bugzilla Error message: couldn't create child process: 720003: index.cgi
two steps is try to fix this issue. 1. Turn off the windowns firewall 2. Register the perl to the sy ...
- Canbus ID filter and mask
Canbus ID filter and mask CANBUS is a two-wire, half-duplex, bus based LAN system that is ‘collision ...
- MySQL5.6的my.ini配置
下面这个配置在现网服务器上跑了两年了,里面多项参数都有调整过,这个算是最终的一个配置,这个配置不一定适合每种项目,仅供参考. 如果MySQL出现异常:Out of memory 需要修改这几个参数: ...
- lodash用法系列(5),链式
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- 从普通函数到对象方法 ------Windows窗口过程的面向对象封装
原文地址:http://blog.csdn.net/linzhengqun/article/details/1451088 从普通函数到对象方法 ------Windows窗口过程的面向对象封装 开始 ...
- finger-guessing game:3增加猜拳次数及猜拳按钮显示
增加猜拳次数及猜拳按钮 //初始化引擎组件 init(50, "div_caiquan", 800, 400, main); //定义游戏层,加载进度层.游戏背景层,结果显示层,出 ...
- Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(3) Date
本章主要介绍Date类,并通过示例学习如何使用它.最后,讲解一下UTC.GMT和时区的关系. Date 介绍 Date 定义 public class Date implements java.io. ...