Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:厦门安全网络教育平台 编辑:程序博客网 时间:2024/05/16 08:33
Given preorder and inorder 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 *preorder, int prebegin, int preend, int *inorder, int inbegin, int inend) { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = preorder[prebegin]; root->left = root->right = NULL; //printf("%d ", root->val); if(prebegin == preend || inbegin == inend) return root; int i; for(i = inbegin; i <= inend && inorder[i] != preorder[prebegin]; i++) ; if(i > inbegin) { root->left = buildTreeCore(preorder, prebegin + 1, prebegin + i, inorder, inbegin, i - 1); } if(i < inend) { root->right = buildTreeCore(preorder, prebegin + i + 1, preend, inorder, i + 1,inend); } return root;}TreeNode *buildTree(int *preorder, int *inorder, int n) { if(preorder == NULL || inorder == NULL) return NULL; TreeNode *root = buildTreeCore(preorder, 0, n - 1, inorder, 0, n - 1); return root;}void Travel(TreeNode *root) { if(root != NULL) { Travel(root->left); Travel(root->right); printf("%d,", root->val); }}void main() { int pre[] = {-1}; int in[] = {-1}; TreeNode* root = buildTree(pre, in, 1); Travel(root); }
0 0
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- 2015—01—23 Shell学习笔记基础(2)
- Mysql复制(Master/Slave实现)
- 从机器学习谈起
- python strip()函数
- 【Android】缩略图Thumbnails
- Construct Binary Tree from Preorder and Inorder Traversal
- 无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误解决办法
- Android EditText监听小数点让用户只能输入小数点后两位
- 4.自动装配:
- 使用python设置超时
- php display_errors和error_reporting的赋值方法
- Cocos2d-x 3.3 动作游戏连续普通攻击判断
- SQL中JOIN和UNION区别、用法及示例介绍
- Android怎么让EditText只显示两位小数(格式化法)