Binary | binary tree

Problem: Sequence front and configured binary sequence preorder

Problem link
Here Insert Picture Description

Problem-solving ideas

Preorder traversal: root, left, right
preorder: left, root, the right
preorder traversal of the root node is the first element in the vector, we can preorder divided into three portions by a root node, left, root right, you can build a recursive

C ++ code

/**
 * 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) {
        //如果没节点了返回NULL
        if(preorder.size() == 0 || inorder.size() == 0) return NULL;
        vector<int>left, right;
        //将inorder中在根节点pre[0]左边的数归入left中
        int i = 0, size = inorder.size();
        while(i < size && inorder[i] != preorder[0]){
            left.push_back(inorder[i]);
            i++;
        }
        //将inorder中在根节点pre[0]右边的数归入right中
        i++;
        while(i < size){
            right.push_back(inorder[i]);
            i++;
        }
        TreeNode *root = new TreeNode(preorder[0]);//建立根节点
        preorder.erase(preorder.begin());//删除第一个元素
        root->left = buildTree(preorder, left);//递归建立左子树
        root->right = buildTree(preorder, right);//递归建立右子树
        return root;//返回根节点
    }
};

Question: from order traversal sequence after sequence and structure of a binary tree

Problem link
Here Insert Picture Description

Problem-solving ideas

1, the value obtained by the last node in the root node after traversing
2, according to the value of the root sequence preorder divided left and right subtrees are stored in the left and right
3, right subtree recursively established, then the establishment of recursively left subtree

C ++ code

/**
 * 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) {
        //如果没节点了返回NULL
        if(inorder.size() == 0 || postorder.size() == 0) return NULL;
        //根据根节点值将中序遍历序列进行分割,左右子树分别存放在left和right中
        vector<int>left, right;
        int rootVal = postorder.back();//根节点值
        //将inorder中在根节点rootVal左边的数归入left中
        int i = 0, size = inorder.size();
        while(i < size && inorder[i] != rootVal){
            left.push_back(inorder[i]);
            i++;
        }
        //将inorder中在根节点rootVal右边的数归入right中
        i++;
        while(i < size){
            right.push_back(inorder[i]);
            i++;
        }
        postorder.pop_back();//在后序遍历序列中删除根节点
        TreeNode *root = new TreeNode(rootVal);//建立根节点
        root->right = buildTree(right, postorder);//注意:先递归建立右子树
        root->left = buildTree(left, postorder);//递归建立左子树
        return root;//返回根节点
    }
};
Published 860 original articles · won praise 270 · views 280 000 +

Guess you like

Origin blog.csdn.net/SongBai1997/article/details/104719583
Recommended