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 ...
随机推荐
- 在Visual Studio中使用类图描述领域模型
右键解决方案,添加一个名称为"Domain Model"的UML类图. 首先站在整体的角度添加类,先不要任何属性. 添加Customer对Wishlist的关联,把"属性 ...
- lufylegend:动画
1.动画1 <script type="text/javascript"> var loader,anime,layer; //初始化画布 init(200, &quo ...
- lufylegend:图形变形2
下面来详细讲解一下drawtriangles函数的使用方法.并且使用drawtriangles函数实现下面这种处理效果 因为这个方法是从AS3移植而来,所以它的使用方法和AS3基本一致,这里是AS3的 ...
- 由pushViewController说起可能出线的各种死法
做苹果开发或者果粉对导航条这个东西应该都不陌生,这咚咚在小小的屏幕上通过一个简单的View的队列管理来做到手机界面的有条理管理,但是开发过程程序员可能碰到各种死法,下面分享一二. ...
- 从零开始,运行一个android例子程序
电脑上连个eclipse都没装,怎么玩android?一穷二白的你, 下面就跟随我,从零开始,一步一步操作,运行我们的第一个android应用程序.我一直相信,学习开发,只有在调试过程中学的是最快的. ...
- 浴血黑帮第三季/全集Peaky Blinders迅雷下载
英文译名 Peaky Blinders (第2季) (2014-8月回归)BBC.本季看点:<浴血黑帮>由<东方的承诺>.<奇异的恩典>编剧斯蒂文·奈特打造,讲述了 ...
- 混乱之子第一季/全集Sons Of Anarchy迅雷下载
本季第一至六季 Sons of Anarchy (2008-2013)看点:<混乱之子>发生在一个虚构的加州小镇(Charming)上,面对毒品贩子和大型土地开发商的步步紧逼,一家黑白两道 ...
- 详解Java多线程编程中LockSupport类的线程阻塞用法
LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了基本的线程同步原语.LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,只有两个函数: p ...
- Plupload设置自定义参数
在HTML 5比较流行的当下,Plupload是文件上传的不二之选,特别是Adobe宣布2020年将停止对Flash的更新支持.本文记录一下如何在上传文件的时候,传递自定义参数. 了解到两种方式,一种 ...
- [转]PHP中替换换行符
FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...