将二叉树拆成链表
来源:互联网 发布:网络课程数学文化答案 编辑:程序博客网 时间:2024/06/07 05:42
问题描述:将一棵二叉树按照前序遍历拆解成为一个假链表
。所谓的假链表是说,用二叉树的 right指针,来表示链表中的 next 指针。
样例
1 \ 1 2 / \ \ 2 5 => 3 / \ \ \ 3 4 6 4 \ 5 \ 6
实验代码:
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
vector<TreeNode*> temp;
void flatten(TreeNode *root) {
// write your code here
if(root==NULL)
return;
tianjia(root);
int i;
for( i=0;i<temp.size()-1;i++)
{
temp[i]->left=NULL;
temp[i]->right=temp[i+1];
}
}
void tianjia(TreeNode* root)
{
if(root==NULL)
return;
temp.push_back(root);
tianjia(root->left);
tianjia(root->right);
}
};
个人感想:循环的时候将树的左子树定义为空。
0 0
- 将二叉树拆成链表
- LintCode: 将二叉树拆成链表
- 将二叉树拆成链表
- lintcode,将二叉树拆成链表
- LintCode 将二叉树拆成链表
- 将二叉树拆成链表-LintCode
- 将二叉树拆成链表
- 将二叉树拆成链表
- lintcode 将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- lintcode将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- 将二叉树拆成链表
- java惰性初始化
- 文章标题
- jQuery闭包函数
- 使用D3制作统计图表、线性图表、水平柱状图表、饼图 -- (四)水平柱状图表+坐标轴
- 模板_tarjan强连通缩点算法
- 将二叉树拆成链表
- 百度地图API+ArcGIS软件—城市出行时空数据可视化
- 基础day06笔记-activity组件
- 继续加油,努力
- 中文编解码问题
- 二、servlet类中@WebServlet 报错,不变色
- C#调用dll的简单demo(2)--反射
- 接口里的default方法
- 类中成员和方法的初始化顺序