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 ...
随机推荐
- UIImageView 详解
1.//设置 圆角 userhead.layer.masksToBounds = YES; userhead.layer.cornerRadius = 6.0; userhead.layer.bord ...
- iReport数据库连接找不到驱动
- java nio 映射文件内容到内存
FileChannel 的一个最好的功能就是能将文件的某个部分直接映射到内存.这要感谢 FileChannel.map() 方法,这个方法有下面三个参数: mode:映射到内存需要指定下面三种模式之一 ...
- 将CAGradientLayer当做mask使用
将CAGradientLayer当做mask使用 效果 源码 https://github.com/YouXianMing/Animations // // CAGradientView.h // M ...
- dip和px的相互转化
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { fina ...
- 将win7电脑无线网变身WiFi热点,让手机、笔记本共享上网
1.以管理员身份运行命令提示符:快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡:运行命令:netsh wlan set hostednetwork mode=allow ssid= ...
- LaTeX使用技巧
使用LaTex的方法: (1)推荐一个手写公式.自动生成LaTex的网站——Web Equation. (2)如果会LaTex,可以直接用在线LaTex编辑 (3)从mathtype转换: 首先打开文 ...
- Java工程Properties配置文件注释中文,会自动转换为其他编码方式问题解决 中文乱码
properties文件中想注释中文,但是写出来后却是 :# /4djf/234/4354/r23df/324d 这种效果 是因为字符编码默认没有设置造成的,以前总是安装插件解决此问题, 但是却牺牲 ...
- iOS:通过Self-Sizing Cells新特性自动计算cell的高度
iOS8 新特性Self-Sizing Cells,自动计算cell的高度 一.简单介绍 UITableView是iOS开发最常用的一个控件,通过代理和数据源方法,几乎能实现各种各样的列表功能.在这里 ...
- RV32C指令集
Risc-V支持16位压缩格式,压缩格式立即数位数更少,能使用的寄存器也比较少,有些指令只能用常用8个整数寄存器(x8-x15)或者(f8-f15). 每个RVC指令都有对应的32位指令,下表列出所有 ...