将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针。

注意事项

不要忘记将左儿子标记为 null,否则你可能会得到空间溢出或是时间溢出。

您在真实的面试中是否遇到过这个题?

Yes
样例

              1
\
1 2
/ \ \
2 5 => 3
/ \ \ \
3 4 6 4
\
5
\
6 思路:采用递归的方法,按照先序遍历,将节点存放起来,放在一个容器中; 然后新建一个头结点指针,遍历容器中的节点,依次将节点连接成一个链表。      但是,要注意将左儿子标记为NULL;
/**
* 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
*/
/*
思路:采用递归的方法,按照先序遍历,先存放起来;
但是,要注意将左儿子标记为NULL;
*/ vector<TreeNode*> vec;
vector<TreeNode*> Node(TreeNode* root,vector<TreeNode*>& vec){
if(root==NULL){
return vec;
}
vec.push_back(root);
if(root->left!=NULL){
Node(root->left,vec);
}
if(root->right!=NULL){
Node(root->right,vec);
}
return vec;
} void flatten(TreeNode *root) {
// write your code here
if(root==NULL){
return;
} Node(root,vec);
root=new TreeNode();//创建新的头结点指针;
for(int i=0;i<vec.size();i++){
//多想想,第一次的写法为啥没有通过
vec[i]->left=NULL;
root->right=vec[i];
root=root->right;
}
}
};


Lintcode---将二叉树拆成链表的更多相关文章

  1. lintcode 453 将二叉树拆成链表

    将二叉树拆成链表   描述 笔记 数据 评测 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 注意事项 不要忘记将左儿子标 ...

  2. lintcode:将二叉树拆成链表

    题目 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 注意事项 不要忘记将左儿子标记为 null,否则你可能会得到空间溢出 ...

  3. LintCode_453 将二叉树拆成链表

    题目 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 样例 1 \ 1 2 / \ \ 2 5 => 3 / \ \ ...

  4. [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

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

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

  6. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

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

  8. leetcode 114二叉树转换成链表

    解法一 可以发现展开的顺序其实就是二叉树的先序遍历.算法和 94 题中序遍历的 Morris 算法有些神似,我们需要两步完成这道题. 将左子树插入到右子树的地方 将原来的右子树接到左子树的最右边节点 ...

  9. 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

随机推荐

  1. NativeXml: A native Delphi XML parser and writer

    http://www.simdesign.nl/xml.html This software component contains a small-footprint Object Pascal (D ...

  2. 读CRecordset

    void CDictCol::LoadDictCol(void) { // 加载数据字典信息 CString cstrSql; cstrSql.Format("SELECT dc.TblID ...

  3. Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Connectio

    严重: StandardWrapper.Throwableorg.springframework.transaction.CannotCreateTransactionException: Could ...

  4. frp, https, http, nginx 多服务, ssl等配置

    server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.ph ...

  5. Mac配置PHP开发环境

    安装环境如下: Mac OS 10.10.1 Apache 2.4.9 PHP 5.5.14 MySQL 5.6.22 Apache配置 在Mac OS 10.10.1中是自带Apache软件的,我们 ...

  6. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

  7. Android 交叉编译程序提示(not found)

    原因是缺少库文件, 解决办法:arm-linux-readelf -a helloword | grep NEEDED 拷贝so文件到安卓下 或者 arm-linux-gcc hello.c -o h ...

  8. 给 Easyui Datagrid 扩展方法

    $.extend($.fn.datagrid.methods, { /** * 更新 非编辑列值 * @param rowIndex : 行索引 * @param cellName : 列索引或列名 ...

  9. Android -- Options Menu,Context Menu,Popup Menu

    Options Menu                                                                           创建选项菜单的步骤: 1. ...

  10. 神奇的container_of

    container_of是linux内核中常用的一个宏,这个宏的功能是,根据某个结构体字段的指针,找到对应的结构体指针. 话不多说,先上源码: /** * container_of - cast a ...