LintCode: Flatten Binary Tree to Linked List
C++
Traverse
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void flatten(TreeNode *root) {
if(!root) return;
vector<TreeNode*> allNodes;
preorder(root, allNodes);
int n = allNodes.size();
for(int i=; i<n-; i++) {
allNodes[i]->left = NULL;
allNodes[i]->right = allNodes[i+];
}
allNodes[n-]->left = allNodes[n-]->right = NULL;
} void preorder(TreeNode *root, vector<TreeNode*> &allNodes) {
if(!root) return;
allNodes.push_back(root);
preorder(root->left, allNodes);
preorder(root->right, allNodes);
}
};
C++
Divide-Conquer
Recursive
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void flatten(TreeNode *root) {
helper(root);
} TreeNode* helper(TreeNode *root) {
if (root == NULL) {
return NULL;
}
if (root->left == NULL && root->right == NULL) {
return root;
}
if (root->left == NULL) {
return helper(root->right);
}
if (root->right == NULL) {
root->right = root->left;
root->left = NULL;//!important
return helper(root->right);
}
//Divide
TreeNode *leftLastNode = helper(root->left);
TreeNode *rightLastNode = helper(root->right); //Conquer
leftLastNode->right = root->right;
root->right = root->left;
root->left = NULL;//!important
return rightLastNode;
}
};
LintCode: Flatten Binary Tree to Linked List的更多相关文章
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- 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: ...
- 31. 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-pl ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- 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-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 ...
随机推荐
- 认识TWICImage类
Graphics 单元新增了 TWICImage 类, 该类的功能源于新增的 Wincodec.pas 单元. WIC 支持的图像有 BMP.PNG.ICO.JPEG.GIF.TIFF.HDP(HDP ...
- JAVA BigDecimal的相加
之前很少使用这样的一个对象BigDecimal,今天在改需求的时候遇到了,结果坑爹的怎么相加最后都为零. 代码如下: BigDecimal totalAmount = new BigDecimal(0 ...
- C#编程(三十八)----------运算符
原文链接: http://blog.csdn.net/shanyongxu/article/details/46877353 运算符 类别 运算符 算术运算符 + - * / 逻辑运算符 & ...
- ORACLE数据库导入的时候出现IMP-00038: 无法转换为环境字符集句柄
数据泵不一致导致的,比如说你用expdp导出来的 用imp导入的时候就会出现这个错误exp导出来的用imp导入expbd导出来的用impdp导入和版本没有关系
- 自定义兼容多种Protobuf协议的编解码器
<从零开始搭建游戏服务器>自定义兼容多种Protobuf协议的编解码器 直接在protobuf序列化数据的前面,加上一个自定义的协议头,协议头里包含序列数据的长度和对应的数据类型,在数据解 ...
- Codeforces Round #131 Div1 B
Problem 给出Ai(i∈[0,9]).表示每一个数字至少须要出现多少次.问有多少个数满足下面三个条件:1. 数至多有N位:2. 数不含有前导0:3. 每一个数 i 出现的次数不少于Ai(mod ...
- fastjson 过滤不需要的字段或者只要某些字段
/* * 第一种:在对象响应字段前加注解,这样生成的json也不包含该字段. * @JSONField(serialize=false) * private String name; */ / ...
- 清道夫第一季/全集Ray Donovan迅雷下载
清道夫 第一季 Ray Donovan Season 1 (2013)本季看点:Ray Donovan.一位专职于为洛杉矶的名人和富豪服务的神秘人士.他可以巧妙的解决这个城市中富豪们的那些最麻烦同时又 ...
- SVG.js 文本绘制整理
1.SVG.Text var draw = SVG('svg1').size(300, 300); //画文字内容展示 //var text = draw.text('中文内容测试\n换行处理'); ...
- ARM、X86/Atom、MIPS、PowerPC
关注Android的时候,有一些CPU架构方面的术语知识,主要有:ARM.X86/Atom.MIPS.PowerPC1)ARM/MIPS/PowerPC均是基于精简指令集(RISC,Reduced I ...