首页 > 学院 > 开发设计 > 正文

106. Construct Binary Tree from Inorder and Postorder Traversal

2019-11-10 17:47:24
字体:
来源:转载
供稿:网友

Q

https://leetcode.com/PRoblems/construct-binary-tree-from-inorder-and-postorder-traversal/

Given inorder and postorder traversal of a tree, construct the binary tree.

A

/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) { if (!inorderSize || !postorderSize || inorderSize != postorderSize) { return NULL; } struct TreeNode *p = (struct TreeNode *)malloc(sizeof(struct TreeNode)); p->val = postorder[postorderSize-1]; int i = 0; while(inorder[i] != postorder[postorderSize-1]) ++i; p->left = buildTree(inorder, i, postorder, i); p->right = buildTree(inorder+i+1, inorderSize-i-1, postorder+i, postorderSize-i-1); return p;}
上一篇:Hd1029

下一篇:我的ACM参赛故事

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表