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 ...
随机推荐
- spoj 7001 Visible Lattice Points莫比乌斯反演
Visible Lattice Points Time Limit:7000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- 2016 Multi-University Training Contest 10 solutions BY BUPT
1001. 一个数组上的两个区间求中位数,可以通过分类讨论直接找到中位数,复杂度O(1).不过本题数据较小,优美的log(n)也可过. 1002. 直接求得阴影面积表达式即可. 1003. 二分完成时 ...
- WebRTC编译详细介绍 (转)
WebRTC技术交流群:234795279 原文地址:http://blog.csdn.net/temotemo/article/details/7056581 WebRTC编译 本人环境: 操作 ...
- 51深入理解C指针之---指针与线程
一.size_t:用于安全表示长度,所有平台和系统都会解析成自己对应的长度 1.定义:size_t类型表示C中任何对象所能表示的最大长度,是个无符号整数:常常定义在stdio.h或stdlib.h中 ...
- #define xxx do{...} while(0) 宏定义
linux内核和其他一些开源的代码中,经常会遇到这样的代码: do{ ... }while(0) 这样的代码一看就不是一个循环,do..while表面上在这里一点意义都没有,那么为什么要这么用呢? 实 ...
- 前端笔记之Vue(七)Vue-router&axios&Vue插件&Mock.js&cookie|session&加密
一.Vue-router(路由) 1.1路由创建 官网:https://router.vuejs.org/zh/ 用 Vue.js + Vue Router 创建单页应用,是非常简单的.使用 Vue. ...
- screen状态变Attached连接会话失败
使用xshell远程登录主机,使用screen命令启动程序运行至后台,意外发现screen session的状态为Attached,使用命令screen -r <session-id>,提 ...
- 高性能内存池NedAlloc
http://www.nedprod.com/programs/portable/nedmalloc/ http://blog.sina.com.cn/s/blog_6f5b220601012x4t. ...
- win10 安装git
http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html 双击exe安装包,在弹出的安全提示中点击“允许”. 安装向导的头两步都 ...
- 【Todo】各种语言里面的for循环 & loop
会的语言多了,不同语言的语法就会混淆.整理了一下. Java里面: 普通的for循环之外: 有以下格式: for (int x : intarr) { } JS里面: }; for (x in per ...