欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-27 11:34:50
...

问题描述(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);
    }
};

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

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

/**
 * 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;
    }
};