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的更多相关文章

  1. [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 ...

  2. 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: ...

  3. 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 ...

  4. 【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 ...

  5. 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 ...

  6. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

  7. 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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. AutoMapper在MVC中的运用02-Decimal转String、集合、子父类映射

    本篇AutoMapper使用场景: ※ Decimal转换成String类型 ※ 源数组转换成目标数组 ※ 源中的集合(数组)属性转换成目标中的集合(数组)属性 ※ 子类父类间的映射 Decimal转 ...

  2. Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)(转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第五篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  3. redis实现异步任务队列

    redis实现异步任务队列 先说思路: 将任务对象序列为JSON字符串,然后推入REDIS缓存,这叫入队. 通过独立的工作线程从REDIS拉出一个任务,这叫出队,工作线程将JSON字符串还原为任务对象 ...

  4. C++关键字之virtual

    from://http://blog.csdn.net/xuyuanfan/article/details/9935533 在C++中是没有接口的,要真正实现java中的interface功能,需要使 ...

  5. SharePoint PowerShell 批量删除遗弃视图

    前言 最近,给SharePoint升级了,然后发现,有一大批视图不需要了,而且,名字是一样的,想着怎么清理,然后,就想到了powershell. powershell 示例: $siteUrl = & ...

  6. IntelliJ IDEA2018.1、2017.3激活

    IntelliJ IDEA2018.1.2017.3破解教程 http://idea.java.sx/ 简单快捷!! ————————————————————————————————————————  ...

  7. 纸牌屋第一季/全集House of Cards迅雷下载

    纸牌屋 第一季 House of Cards Season 1 (2013) 本季看点:经过数轮激烈角逐,新一届美国总统加勒特·沃克(迈克·吉尔 Michael Gill 饰)诞生,自称水管工的众议院 ...

  8. Android NDK:Aborting..Stop的处理方法

    在eclipse中配置cocos2d-x的android环境时,遇到这样的错误提示 网上搜索了一下,说是在NDK_MODULE_PATH环境变量下未找到所需要的Android.mk文件,后来仔细研究了 ...

  9. Java并发编程的艺术(七)——Executors

    Executors框架简介 Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.cocurrent 包下,通过该框架来控制线程的启动.执行和关闭,可以简化并发 ...

  10. pip 安装错误 'ascii' codec can't encode characters

    安装 python-dev既可解决 apt-get install python-dev