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 ...
随机推荐
- 关于console.log() 打印得引用类型得数据得相关问题
console.log()打印出来得是这个引用类型最终得结果,而不是在打印得时候当前得值 ,b:} console.log(json) json.a = ; 如上 ,打印得将是 {a:3,b:2} ...
- C语言扩展题
1.使用cmake来创建c语言工程 2.使用gcc来编译源代码 3.下载redis,并且编译运行redis(注:redis目前是c语言编写的,而且是主要是linux平台,在windows平台编译比较麻 ...
- TextReader 和StreamReader
TextReader 和StreamReader 目录: 为什么要介绍 TextReader? TextReader的常用属性和方法 TextReader 示例 从StreamReader想到多态 简 ...
- POJ 2577: Interpreter
简略解题报告 Description A certain computer has 10 registers and 1000 words of RAM. Each register or RAM l ...
- vscode golang配置说明
一.vscode-go插件安装 go 1.10.2 https://golang.org/dl/ 需要墙 vscode 1.23.1 https://code.visualstudio.com/ vs ...
- LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...
- Unity3d Inspector面板实现set/get访问器
简单说一下属性和字段的区别:字段就是成员变量,而属性确实提供给外部访问内部成员变量的接口.之所以会有属性的出现,就是为了避免外部对类的成员的直接访问,通俗的说就是OOP中的封装思想. using Un ...
- 采集网站特殊文件Meta信息
采集网站特殊文件Meta信息 元(Meta)信息是描述文件的属性的特殊信息,如文件的所有者.联系方式.机构名.邮件地址等信息.而网站中常常会有共享的文档文件,如PDF.Excel.Word.这些文 ...
- spring事务详细理解
数据并发的问题 一个数据库可能拥有多个访问客户端,这些客户端都可以并发方式访问数据库.数据库中的相同数据可能同时被多个事务访问,如果没有采取必要的隔离措施,就会导致各种并发问题,破坏数据的完整性.这些 ...
- 【spring cloud】Feign使用填坑
引用地址:https://blog.csdn.net/liuchuanhong1/article/details/54728681 问题一: 在前面的示例中,我们讲过 @RequestMapping( ...