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 ...
随机推荐
- mysql返回记录的ROWNUM(转)
set @rownum = 0; select (@rownum := @rownum + 1) as rownum, name, scores from user order by scores ...
- adb pull adb push
adb pull:数据从真机到计算机 adb push: 数据从计算机到真机 使用方法: 在android开发环境的sdk--platform tools中安装了adb,在该目录下运行“adb pul ...
- json字符串转为json对象-jQuery.parseJSON()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java游戏开发基础Swing之JCheckBox
© 版权声明:本文为博主原创文章,转载请注明出处 1.复选框(JCheckBox) 使用复选框可以完成多项选择.Swing中的复选框与AWT中的复选框相比,优点是Swing复选框中可以添加图片 JCh ...
- EasyNetQ操作RabbitMQ(高级消息队列)
RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).写消息队列的时候用RabbitMQ比较好,但是写的时候需要自己封装下,自己的封装,就需要对RabbitM ...
- TCP粘包处理通用框架--C代码
说明:该文紧接上篇博文“ linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现 ”讲来 (1)TCP粘包处理数据结构设计 #define MAX_MSG_LEN 65535 ty ...
- windows_64下python下载安装Numpy、Scipy、matplotlib模块
本文应用的python3.6.3及其对应的Numpy.Scipy.matplotlib计算模块的cp36版本,其中Numpy是需要MKL版本的Numpy,这是后续安装Scipy的需要(本机系统win7 ...
- u-boot README--Memory Management&initialize
Memory Management:------------------ U-Boot runs in system state and uses physical addresses, i.e. t ...
- zabbix 自定义脚本监控activemq
1. 编写获取activemq队列积压消息(check-amq.sh) #!/bin/bash QUEUENAME=$ MQ_IP='172.16.1.56' curl -uadmin:admin h ...
- shiro集成encache
针对多频次或者几乎不变的大数量的数据,我们可以通过缓存来实现,具体的比如说权限认证,这个,每次操作都需要权限认证,所以,这里添加encache注解.具体的认证过程是: 1,用户第一次访问用户权限信息, ...