Flatten Binary Tree to Linked List [LeetCode]
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.
Solution:
void flatten(TreeNode *root) {
stack<TreeNode *> nodes;
if(root == NULL)
return;
nodes.push(root);
TreeNode * pre = root;
while(nodes.size() != ){
TreeNode * node = nodes.top();
nodes.pop();
if(node->right != NULL)
nodes.push( node->right);
if(node->left != NULL)
nodes.push( node->left);
pre->left = NULL;
if(node != root){
pre->right = node;
pre = pre->right;
}
}
}
Flatten Binary Tree to Linked List [LeetCode]的更多相关文章
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
- Flatten Binary Tree to Linked List ——LeetCode
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- Flatten Binary Tree to Linked List leetcode java
题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- 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:Flatten Binary Tree to Linked List 解题报告
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- [LeetCode]Flatten Binary Tree to Linked List题解(二叉树)
Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For e ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
随机推荐
- spring boot servlet 注入
spring boot 注入servlet的方法是借助ServletRegistrationBean这个类 例子如下: 先建一个servlet import java.io.IOException; ...
- Linux:-拷贝或传送文件的技巧
<---拷贝目录如何做到排除文件?常用命令cp,用法比较LOW---> tar -cf - ./* --exclude="nohup.out" | (cd /opt/ ...
- Spring配置文件详解
转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...
- Windows光标形状
::SetCursor( LoadCursor(NULL, IDC_XXX) ); IDC_ARROW (plain) IDC_HELP (arrow + question mark) IDC_APP ...
- python网络编程-socket
python提供了两个socket模块 Socket,它提供了标准的BSD Sockets API SocketServer,它提供了服务器中心类,可以简化网络服务器的开发 下面先说socket模块 ...
- Maven安装配置使用
Maven介绍 Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理 ...
- HDU1232 畅通工程 并查集
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Json解析实例
using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Win ...
- shell随机输出一人的学号与姓名
如果要多输出,可以分成*组,然后从每组中输出一个(这里是分为3组)
- Java-密码加密
介绍两种密码加密的方法: 这两种很常见可以再百度随意找到. 1.摩斯密码:说道密码加密不得不提的方法.很是经典. 首先说一下他的对照表,直接上图. 核心思想就是替换明文密码,将字符对应的替换成-与.两 ...