Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Subscribe to see which companies asked this question
/** * 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) { int i; struct TreeNode *Node; >= inorderSize){ return NULL; } ; i < inorderSize; i++){ ]){ break; } } Node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); Node->val = postorder[postorderSize - ]; Node->left = buildTree(inorder, i, postorder, i); Node->right = buildTree(inorder + i + , inorderSize - i - , postorder + i, postorderSize - i - ); return Node; }