18代码随想录训练营day18|二叉树part05

1、找树左下角的值

513. 找树左下角的值 - 力扣(LeetCode)
class Solution {
public:
    int result;
    int maxDepth = INT_MIN;
    void value(TreeNode* root, int depth) {
        if (root->left == NULL && root->right == NULL) {
            if (depth > maxDepth) {
                maxDepth = depth;
                result = root->val;
            }
            return ;
        }
        if (root->left) value(root->left, depth + 1);
        if (root->right) value(root->right, depth + 1);
        return ;
    }
    int findBottomLeftValue(TreeNode* root) {
        value(root, 0);
        return result;
    }
};

2、路径总和

112. 路径总和 - 力扣(LeetCode)

class Solution {
public:
    bool result(TreeNode* root, int targetSum) {
        if (root->left == NULL && root->right == NULL) {
            if (targetSum == 0) return true;
            return false;
        }
        bool left = false;
        bool right = false;
        if (root->left) left = result(root->left, targetSum - root->left->val);
        if (root->right) right = result(root->right, targetSum - root->right->val);
        return left || right;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return false;
        return result(root, targetSum - root->val);
    }
};

 113. 路径总和 II - 力扣(LeetCode)

class Solution {
public:
    void resultFunction(TreeNode* root, int targrt, vector& vec, vector>& result) {
        if (root->left == NULL && root->right == NULL) {
            if (targrt == 0) {
                result.push_back(vec);
                return ;
            }
            return ;
        }
        if (root->left) {
            vec.push_back(root->left->val);
            resultFunction(root->left, targrt - root->left->val, vec, result);
            vec.pop_back();
        }
        if (root->right) {
            vec.push_back(root->right->val);
            resultFunction(root->right, targrt - root->right->val, vec, result);
            vec.pop_back();
        }
    }
    vector> pathSum(TreeNode* root, int targetSum) {
        vector> result;
        vector vec;
        if (root == NULL) return result;
        vec.push_back(root->val);
        resultFunction(root, targetSum - root->val, vec, result);
        return result;
    }
};

3、中序遍历与后续遍历构造二叉树

 106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

class Solution {
public:
    TreeNode* traversal(vector& inorder, vector&postorder) {
        // 终止条件,无结点
        if (inorder.size() == 0) return NULL;
        // 后序遍历,取数组中的最后一个元素
        int postValue = postorder[postorder.size() - 1];
        TreeNode* root = new TreeNode(postValue);
        // 终止条件,叶子结点
        if (inorder.size() == 1) return root;
        // 找到中序遍历的切割点
        int delimiterIndex;
        for (int i = 0; i < inorder.size(); i++) {
            if (postValue == inorder[i]) {
                delimiterIndex = i;
                break;
            }
        }
        // 切割中序遍历数组,用到切割点delimiterIndex
        vector leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
        vector rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end());
        // 切割后序遍历数组,用到中序遍历数组的长度
        vector leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
        vector rightPostorder(postorder.begin() + leftInorder.size(), postorder.begin() + leftInorder.size() + rightInorder.size());

        root->left = traversal(leftInorder, leftPostorder);
        root->right = traversal(rightInorder, rightPostorder);
        return root;
    }
    TreeNode* buildTree(vector& inorder, vector& postorder) {
        return traversal(inorder, postorder);
    }
};

 4、中序遍历和前序遍历构造二叉树

105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)

class Solution {
public:
    TreeNode* travsersal(vector& preorder, vector& inorder) {
        if (inorder.size() == 0) return NULL;
        int preValue = preorder[0];
        TreeNode* root = new TreeNode(preValue);
        int delimiterIndex;
        for (int i = 0; i < inorder.size(); i++) {
            if (preValue == inorder[i]) {
                delimiterIndex = i;
                break;
            }
        }
        vector leftPreorder(preorder.begin() + 1, preorder.begin() + delimiterIndex + 1);
        vector rightPreorder(preorder.begin() + delimiterIndex + 1, preorder.end());
        vector leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
        vector rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end());
        root->left = travsersal(leftPreorder, leftInorder);
        root->right = travsersal(rightPreorder, rightInorder);
        return root;
    }
    TreeNode* buildTree(vector& preorder, vector& inorder) {
        return travsersal(preorder, inorder);
    }
};

你可能感兴趣的:(代码随想录训练营,算法,数据结构,c++)