leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List
Total Accepted: 25034 Total
Submissions: 88947My Submissions
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
题意:把一棵二叉树变为链表,要求原地进行转变。
思路:dfs
能够注意到。在链表表示中,每一个节点的下一个节点是二叉树的先序遍历中的下一个节点。所以问题就转换为先序遍历了。
复杂度:时间O(n),空间O(log n)
TreeNode *cur;
void flatten(TreeNode *root) {
if(!root) return;
TreeNode *left = root->left;
TreeNode *right = root->right;
if(cur){
cur->left = NULL;
cur->right = root;
}
cur = root;
flatten(left);
flatten(right);
}
leetcode dfs Flatten Binary Tree to Linked List的更多相关文章
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 【LeetCode】Flatten Binary Tree to Linked List
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...
- [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 ...
- [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][JAVA] 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】Flatten Binary Tree to Linked List (middle)
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 ----- 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 ...
随机推荐
- Nginx合并静态资源,以减轻web服务器压力
Nginx concat模块由淘宝开发,并且淘宝已经在使用这个Nginx模块.这个模块类似于apache中的modconcat.如果需要使用它,需要使用两个?问号.Nginx concat通过合并静态 ...
- 【NOIP2016练习】T2 花花的聚会 (树形DP,倍增)
题意: 花花住在 H 国.H 国有 n 个城市,其中 1 号城市为其首都.城市间有 n 1 条单向道路.从任意一个城市出发,都可以沿着这些单向道路一路走到首都.事实上,从任何一个城市走到首都的路径是唯 ...
- 标准C程序设计七---75
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Win32 绘制RGB三原色图案
以前看到三原色的图案,一直很好奇是如何画出来.后来终于搞清楚了,其实很简单,实际上就是RGB三个分量的"位与"运算. 下面给出Win32绘制三原色图案的例子,特此记录在此: #in ...
- [笔记][Web]利用JS生成博文目录及CSS定制博客
0. 简介 进入到cnblog这个大园子以后,和其他的一些博客网站比起来,少了些小功能,比如旁边CSDN上的目录.不过好在大神辈出,博客园可以通过申请JS权限来进行目录的生成. 由于本人在JS以及CS ...
- 洛谷—— P2183 巧克力
https://www.luogu.org/problemnew/show/P2183 题目描述 佳佳邀请了M个同学到家里玩.为了招待客人,她需要将巧克力分给她的好朋友们.她有N(1<=N< ...
- Parallel Database for OLTP and OLAP
Parallel Database for OLTP and OLAP Just asurvey article on materials on parallel database products ...
- 四个很好的开源app项目
Open Source and the iOS App Store Today, we are open-sourcing 4 iOS apps: ThatInbox, an email client ...
- netd ResponseCode
100 Requestion action was initiated; expect another reply before proceeding with a new command. 200 ...
- Python学习笔记8:标准库之正則表達式
Python拥有强大的标准库.从如今起,開始学习标准库中提供的一些经常使用功能. 首先看正則表達式(regular expression),它的主要功能是从字符串(string)中通过特定的模式(pa ...