Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:32寸网络电视价格表 编辑:程序博客网 时间:2024/05/17 06:26
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
#include<stdio.h>#include<stdlib.h>typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;}TreeNode;TreeNode *buildTreeCore(int *inorder, int inbegin, int inend, int *postorder, int postbegin, int postend) { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = postorder[postend]; root->left = root->right = NULL; if(inbegin == inend || postbegin == postend) return root; int i; for(i = inend; i >= inbegin && postorder[postend] != inorder[i]; i--) ; if(i < inend) { root->right = buildTreeCore(inorder, i + 1, inend, postorder, postbegin + i - inbegin, postend - 1); } if(i > inbegin) { root->left = buildTreeCore(inorder, inbegin, i - 1, postorder, postbegin, postbegin + i - inbegin - 1); } return root;}TreeNode *buildTree(int *inorder, int *postorder, int n) { if(inorder == NULL || postorder == NULL) return NULL; TreeNode *root = buildTreeCore(inorder, 0, n - 1, postorder, 0, n - 1);}void Travel(TreeNode *root) { if(root != NULL) { printf("%d, ", root->val); Travel(root->left); Travel(root->right); }}void main() { int in[] = {1, 2, 3, 4, 5, 6}; int post[] = {1, 2, 4, 5, 6, 3}; TreeNode *root = buildTree(in, post, 6); Travel(root); printf("\n");}
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- xiezhifeng的博客
- artTemplate 简洁语法版
- ffmpeg编译选项汇总
- ExpandableListView嵌套ListView导致ListView不能完全显示数据的问题
- java中的拷贝文件FileChannel
- Construct Binary Tree from Inorder and Postorder Traversal
- eclipse与myeclipse恢复已删除的文件和代码
- API管理
- 把Tomcat做成系统服务自动启动
- Majority Element
- PHP程序员的技术成长规划
- php中的设计模式之--代理模式
- Cocos2d-x游戏开发——CCSequence&CCSpawn
- UML基本架构建模--关联的通用建模技术(三)