Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} node *_buildtree(int pre[], int post[], int &preindex, int l, int h, int size) {
if (preindex >= size || l > h) return NULL;
node *root = new node(pre[preindex++]);
if (l == h) return root;
int i = l;
for (; i <= h; i++) if (pre[preindex] == post[i]) break;
if (i <= h) {
root->left = _buildtree(pre, post, preindex, l, i, size);
root->right = _buildtree(pre, post, preindex, i+, h, size);
}
return root;
} node *buildtree(int pre[], int post[], int size) {
int preindex = ;
return _buildtree(pre, post, preindex, , size-, size);
} int main() {
int pre[] = {, , , , , , , , };
int post[] = {, , , , , , , , };
node *root = buildtree(pre, post, );
prints(root);
return ;
}
Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals的更多相关文章
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- LC 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...
随机推荐
- Burp Suite基本用法
从上一篇已经知道Burp Suite安装.启动方法,本章将会阐述Burp Suite抓包.重放.爆破.双参数爆破.爬虫等基本用法.同博客园看到一篇描述Burp Suite界面各个字段和按钮作用,感兴趣 ...
- 【SpringMVC学习05】SpringMVC中的参数绑定总结——较乱后期准备加入 同一篇幅他人的参数绑定
众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在这一篇博文中,我将总结一下springm ...
- 使用ALAssetsLibrary读取所有照片
一. ALAssetsLibrary 介绍 ALAssetsLibrary 提供了访问iOS设备下”照片”应用下所有照片和视频的接口: 从 ALAssetsLibrary 中可读取所有的相册数据,即 ...
- linux 内存分析
http://blog.yufeng.info/archives/2456 这篇文章不错 值得看 http://www.361way.com/memory-analysis/5018.html
- SpringCloud系列二:硬编码实现简单的服务提供者与服务消费者
从本文开始,以一个电影售票系统为例讲解Spring Cloud 1. 版本 jdk:1.8 SpringBoot:2.0.0.RELEASE SpringCloud:Finchley.M8 2. 系统 ...
- java之CGLIB动态代理
© 版权声明:本文为博主原创文章,转载请注明出处 CGLIB动态代理: CGLIB动态代理就是对指定的类生成一个子类,覆盖其中所有的方法并环绕增强 优势: - 1. 业务类只需要关注业务逻辑本身,保证 ...
- void 指针的转换
不论什么类型的指针都能够显式转换为void类型,且不会丢失数据.例如以下面程序: #include<stdio.h> int main(void) { short a=5; void *p ...
- jsp 页面导出excel时字符串数字变成科学计数法的解决方法
web导出excel数据格式化 原文地址:http://www.cnblogs.com/myaspnet/archive/2011/05/06/2038490.html 当我们把web页面上的数据 ...
- C语言中的main函数以及main函数是如何被调用的
main函数是C语言中比较特殊的函数,C程序总是从main函数开始执行,main函数的原型是: int main(int argc, char *argv[]); 其中argc是命令行参数的个数,ar ...
- 第一篇: Ansible 介绍
应用场景: BOSS:运维帮忙把所有的服务器tomcat 重启一下,谢谢!(tomcat 服务有2K台) 运维:………… 运维: 啪啪啪啪啪啪啪啪..........(键盘的声音响彻办公室) B ...