leetcode【105-106】Construct Binary Tree from Inorder and Preorder Traversal && Postorder Traveresal

问题描述(105):

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

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

For example, given

preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

源码(105):

和剑指offer第7题是一样的。剑指offer总纲。常规做法,时间和空间都一般。

/**
 * 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* constructTree(vector<int>& preorder, vector<int>& inorder, int l1, int r1, int l2, int r2){
        TreeNode *result = new TreeNode(preorder[l1]);
        if(l1>=r1){
            return result;
        }
        int index = l2;
        while(preorder[l1]!=inorder[index])     index++;
        int leftlen = index-l2;
        if(leftlen>0)
            result->left = constructTree(preorder, inorder, l1+1, l1+leftlen, l2, index-1);
        if(leftlen<r1-l1)
            result->right = constructTree(preorder, inorder, l1+leftlen+1, r1, index+1, r2);
        return result;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size()==0 || inorder.size()==0)   return NULL;
        return constructTree(preorder, inorder, 0, preorder.size()-1, 0, inorder.size()-1);
    }
};

非递归的做法就很高效了,还是用栈。

/**
 * 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>& preorder, vector<int>& inorder) {
        int n = preorder.size();
        if(n == 0) return NULL;
        stack<TreeNode*> st;
        int i=0, j=0;
        TreeNode* result = new TreeNode(preorder[i++]);
        st.push(result);
        while(true){
            if(inorder[j] == st.top()->val){
                TreeNode* tmp = st.top();
                st.pop();
                if(++j>=n)  break;
                if(!st.empty() && st.top()->val==inorder[j])    continue;
                tmp->right = new TreeNode(preorder[i++]);
                st.push(tmp->right);
            }
            else{
                TreeNode* node = new TreeNode(preorder[i++]);
                st.top()->left = node;
                st.push(node);
            }
        }
        return result;
    }
};

问题描述(106):

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

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

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

源码(106):

首先还是看递归大法:

/**
 * 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* constructTree(vector<int>& inorder, vector<int>& postorder, int l1, int r1, int l2, int r2){
        TreeNode* result = new TreeNode(postorder[r2]);
        if(l1==r1)  return result;
        int index = l1;
        while(inorder[index] != postorder[r2])  index++;
        // cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<" "<<index<<endl;
        int leftlen = index-l1;
        if(index>l1){
            result->left = constructTree(inorder, postorder, l1, index-1, l2, l2+leftlen-1);
        }
        if(r1>index && r2>l2+leftlen){
            result->right = constructTree(inorder, postorder, index+1, r1, l2+leftlen, r2-1);
        }
        return result;
    }
    
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.empty())     return NULL;
        return constructTree(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
    }
};

非递归的效率好了很多:

/**
 * 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) {
        if(inorder.empty())     return NULL;
        stack<TreeNode*> st;
        int n = inorder.size(), i=n-1, j=n-1;
        TreeNode *result = new TreeNode(postorder[j--]);
        st.push(result);
        while(true){
            if(inorder[i]==st.top()->val){
                TreeNode* tmp = st.top();
                st.pop();
                if(--i<0)   break;
                if(!st.empty() && st.top()->val==inorder[i])     continue;
                tmp->left = new TreeNode(postorder[j--]);
                st.push(tmp->left);
            }
            else{
                TreeNode *node = new TreeNode(postorder[j--]);
                st.top()->right = node;
                st.push(node);
            }
        }
        return result;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值