您的位置:首页 > 编程语言 > C语言/C++

106. Construct Binary Tree from Inorder and Postorder Traversal

2016-05-28 18:22 405 查看
Given inorder and postorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
}

TreeNode* createTree(vector<int>&  inorder, int ib, int ie, vector<int>& postorder, int pb, int pe){
if(ie < ib || pe < pb)
return NULL;
TreeNode* t = new TreeNode(postorder.at(pe));
int pos = 0;
for(int i = ie; i >=ib; i--){
if(inorder.at(i) == t->val){
pos = i;
break;
}
}
t->right = createTree(inorder, pos+1, ie, postorder, pos, pe-1);
t->left = createTree(inorder, ib, pos-1, postorder, pb, pos-1);
return t;
}
};


2.对以上的t->right 和t->left做了修改
在上面,我直接将pos代替pe-(ie-pos),将pos-1代替pe-(ie-pos)-1,这个犯了很大的错误,就是默认pe和ie的位置是一样的,这是非常不正确的,牢记!!!

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
}

TreeNode* createTree(vector<int>&  inorder, int ib, int ie, vector<int>& postorder, int pb, int pe){
if(ie < ib || pe < pb)
return NULL;
TreeNode* t = new TreeNode(postorder.at(pe));
if(ie == ib || pe == pb)
return t;
int pos = 0;
for(int i = ie; i >=ib; i--){
if(inorder.at(i) == t->val){
pos = i;
break;
}
}
t->right = createTree(inorder, pos+1, ie, postorder, pe-(ie-pos), pe-1);
t->left = createTree(inorder, ib, pos-1, postorder, pb, pe-(ie-pos)-1);
return t;
}
};

3.另一种方法没去看,先贴代码,java的,以下是别人的解释:

The core idea is: Starting from the last element of the postorder and inorder array, we put elements
from postorder array to a stack and each one is the right child of the last one until an element in postorder array is equal to the element on the inorder array. Then, we pop as many as elements we can from the stack and decrease the mark in inorder array
until the peek() element is not equal to the mark value or the stack is empty. Then, the new element that we are gonna scan from postorder array is the left child of the last element we have popped out from the stack.

public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 || postorder.length == 0) return null;
int ip = inorder.length - 1;
int pp = postorder.length - 1;

Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null;
TreeNode root = new TreeNode(postorder[pp]);
stack.push(root);
pp--;

while (pp >= 0) {
while (!stack.isEmpty() && stack.peek().val == inorder[ip]) {
prev = stack.pop();
ip--;
}
TreeNode newNode = new TreeNode(postorder[pp]);
if (prev != null) {
prev.left = newNode;
} else if (!stack.isEmpty()) {
TreeNode currTop = stack.peek();
currTop.right = newNode;
}
stack.push(newNode);
prev = null;
pp--;
}

return root;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息