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 ...
随机推荐
- java 中MAP的按照进入顺序遍历与无序遍历
public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...
- OC-《购票系统》
来个命令行的购票系统 --1-- 需求分析 1.1 分析 1.2 功能分析 1.3 流程分析 --2-- 原型展示 2.1 界面原型 --3-- 系统设计 3.1 类设计 3.2 框架模块设计 --4 ...
- xcopy /r /y "$(TargetPath)" "$(ProjectDir)"..\CMSAdmin\DLL\
作用:1.所有都生成这里容易管理 2.tfs获取的时候不会有出问题 3.如果都是引用项目 会存在先后顺序 也会导致生成代码的时候出问题
- Struts框架2ActionError类 内部资料 请勿转载 谢谢合作
ActionError类从不独立进行错误处理,它们总是被存储在ActionErrors对象中.ActionErrors对象保存ActionError类的集合以及它们特定的属性值,我们可以使用自己定义的 ...
- awk 高级技巧
netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a ...
- mysql安装和mysql图形界面安装以及文本文件导入mysql
本人大一大二一直使用windows系统,被微软爸爸给惯坏了,一看到cmd命令行就吓尿.现在用ubuntu,每一个操作都是语句,也是得到锻炼(个jb). ubuntu安装mysql和python代码 s ...
- Duiib 创建不规则窗口(转载)
方法一: 转载:http://blog.csdn.net/chenlycly/article/details/46447297 转载:http://blog.csdn.net/harvic880925 ...
- oracle 锁表问题
oracle执行表数据更新的时候,会遇到锁表问题,比方说,会遇到这样的问题,主要原因是这张表被其他人占用,修改数据没有提交.oracle为了避免脏数据的产生,在其安全机制下,锁住该表. 执行如下操作, ...
- exel按行查找或按列查找
表1:sheet1 1). 在表1中找出ID号位0928的基因相对应的数值 在相对应的单元格输入:B2=VLOOKUP(A:A,Sheet1!A:D,3,0) 既可以得到: ············· ...
- gradle相关配置内容解析
gradle 项目的构建工具,基于groovy语言.主要用于管理依赖包. as中一般将gradle下载在C:\Documents and Settings<用户名>.gradle\wrap ...