代码随想录算法训练营第十六天| 513.找树左下角的值 112.路径总和 113.路径总和II 106.从中序与后序遍历序列构造二叉树

 513.找树左下角的值

        这里用了前序遍历,参照了递归的模板,把保存符合条件的节点(最底层最左边的节点)。注意点:遍历不能传入depth++或者++depth,因为这样会修改栈中depth的值,随着递归depth的值会直接改变。

class Solution {
public:
    int maxDepth = INT_MIN;
    int res;
    void tranversal(TreeNode* root, int depth) {
        if(root->left == nullptr && root->right == nullptr) {
            if(maxDepth < depth){
                maxDepth = depth;
                res = root->val;
            }
            return;
        }

        if(root->left){
           tranversal(root->left,depth+1); 
        }
        if(root->right){
           tranversal(root->right,depth+1); 
        }
        return;
    }
    int findBottomLeftValue(TreeNode* root) {
        tranversal(root,0);
        return res;
    }
};

112.路径总和

         这题是经典的回溯应用,思路写在了相关注释上面。

class Solution {
public:
    vector<int> res; // 存储当前路径
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return false; // 空节点直接返回

        res.push_back(root->val); // 当前节点加入路径
        targetSum -= root->val;   // 减去当前节点值

        // 判断是否是叶子节点,且总和等于目标值
        if (!root->left && !root->right && targetSum == 0) {
            res.pop_back(); // 回溯
            return true;
        }

        // 递归检查左子树和右子树
        if (hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum)) {
            res.pop_back(); // 回溯
            return true;
        }

        res.pop_back(); // 回溯
        return false;
    }
};

113.路径总和II

        和上题不同之处在于,上题只要有一条路径符合条件就返回true。但是这题要求记录所有符合条件的路径,所以遍历要用void。整体思路还是回溯法。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> Psum;
    void isPathSum(TreeNode* root, int targetSum,vector<int> &res) {
        if(!root) return;
        if(!root->left && !root->right){
            if(targetSum == 0) {
                Psum.push_back(res);               
            }
            return;
        }

        if(root->left) {
            targetSum -= root->left->val;
            res.push_back(root->left->val);
            isPathSum(root->left,targetSum,res);
            res.pop_back();
            targetSum += root->left->val;
        }
        if(root->right) {
            targetSum -= root->right->val;
            res.push_back(root->right->val);
            isPathSum(root->right,targetSum,res);
            res.pop_back();
            targetSum += root->right->val;
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<int> res;
        if(!root) return {};
        res.push_back(root->val);
        isPathSum(root,targetSum - root->val,res);
        return Psum;
    }
};

106.从中序与后序遍历序列构造二叉树

        这题不看答案真写不出,思路太巧妙了。二叉树本质由一个个子二叉树构成, 子树遵循的遍历规律符合整体规律,所以把根的左节点和右节点都看作新的根节点,

        root = traversal (inorder, postorder)
        root->left = traversal(leftInorder, leftPostorder);
        root->right = traversal(rightInorder, rightPostorder);

确认了思路后就要找出上面的参数。由于后序遍历的最后一个元素为root的值,先找出后再在中序遍历数组里找出其位置作为分割点。 左边为leftInorder,右边为rightInorder;后序遍历和中序遍历的子数组长度相同,所以又能得出leftPostoder和rightPostorder。直接带入递归就行。

class Solution {
private:
    TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
        if (postorder.size() == 0) return NULL;

        // 后序遍历数组最后一个元素,就是当前的中间节点
        int rootValue = postorder[postorder.size() - 1];
        TreeNode* root = new TreeNode(rootValue);

        // 叶子节点
        if (postorder.size() == 1) return root;

        // 找到中序遍历的切割点
        int delimiterIndex;
        for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
            if (inorder[delimiterIndex] == rootValue) break;
        }

        // 切割中序数组
        // 左闭右开区间:[0, delimiterIndex)
        vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
        // [delimiterIndex + 1, end)
        vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );

        // postorder 舍弃末尾元素
        postorder.resize(postorder.size() - 1);

        // 切割后序数组
        // 依然左闭右开,注意这里使用了左中序数组大小作为切割点
        // [0, leftInorder.size)
        vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
        // [leftInorder.size(), end)
        vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());

        root->left = traversal(leftInorder, leftPostorder);
        root->right = traversal(rightInorder, rightPostorder);

        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return traversal(inorder, postorder);
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值