补签打卡day21

题目1:513. 找树左下角的值 - 力扣(LeetCode)

这道题用层序遍历很简单,层序遍历是用 队列 !!!实现 

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        // 层序遍历使用queue队列解决
        queue<TreeNode*> qu;
        int reslut;
        if (root != NULL) qu.push(root);
        while(!qu.empty()) {
            int size = qu.size();
            for (int i = 0;i < size;i++) {
                TreeNode* tmp = qu.front();
                qu.pop();
                if (i == 0) reslut = tmp->val;
                if (tmp->left) qu.push(tmp->left);
                if (tmp->right) qu.push(tmp->right);
            }
        }
        return reslut;
    }
};

递归算法的思路就是去找最大深度,然后深度变化之后就进行值得改变,因为顺序是按左右来的所以不用考虑最左边得值到底是深度最大得左子叶还是右子叶。

代码:

class Solution {
public:
    int max = 0;
    int rel;
    void func(TreeNode* node, int depth) {
        if(node->left == NULL && node->right == NULL) {
            if(depth > max) {
                max = depth;
                rel = node->val;
            }
        }
        if(node->left) func(node->left, depth + 1);
        if(node->right) func(node->right, depth + 1);
    }
    int findBottomLeftValue(TreeNode* root) {
        func(root, 1);
        return rel;
    }
};

题目2:112. 路径总和 - 力扣(LeetCode)

这道题我的思路就是把所有的路径都记下来,之后去判断所有路径里是否有路径之和==target的 ,AC过了,代码如下:

class Solution {
public:
    int sum = 0;
    vector<vector<int>> all;
    vector<int> path; 
    void findpath(TreeNode* node) {
        path.push_back(node->val);
        if(node->left == NULL && node->right == NULL) {
            all.push_back(path);
            return;
        }
        if(node->left) {
            findpath(node->left);
            path.pop_back();
        }
        if(node->right) {
            findpath(node->right);
            path.pop_back();
        }
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root) findpath(root);
        else return false;
        for(int i = 0;i < all.size();i++) {
            int sum = 0;
            for(int j = 0;j < all[i].size();j++) {
                sum += all[i][j];
            }
            if(sum == targetSum) return true;
        }
        return false;
    }
};

代码随想录里的解法:通过判断叶子节点时sum 是否减到 0 同时,如果递归的时候路径之和== target 就一路返回true.

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

题目3:113. 路径总和 II - 力扣(LeetCode)

这个我还用最开始的思路,直接记录所有路径,然后把复合的再加入,但是这个相较于代码随想录上的算法,会多for循环去判断,可以直接在递归函数里加入 int sum 然后递减,如果 sum == 0证明找到了路径,就加入到结果里。

class Solution {
public:
    vector<vector<int>> all;
    vector<vector<int>> reslut;
    vector<int> path; 
    void findpath(TreeNode* node) {
        path.push_back(node->val);
        if(node->left == NULL && node->right == NULL) {
            all.push_back(path);
            return;
        }
        if(node->left) {
            findpath(node->left);
            path.pop_back();
        }
        if(node->right) {
            findpath(node->right);
            path.pop_back();
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(root) findpath(root);
        else return reslut;
        for(int i = 0;i < all.size();i++) {
            int sum = 0;
            for(int j = 0;j < all[i].size();j++) {
                sum += all[i][j];
            }
            if(sum == targetSum) reslut.push_back(all[i]);
        }
        return reslut;

    }
};

题目4:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)

精髓就在根据后序确定中节点,然后去中序里找到index,然后切割中序和后序数组进行递归,代码如下:

class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(postorder.size() == 0) return NULL;
        TreeNode* node = new TreeNode(postorder[postorder.size() - 1]);
        if(postorder.size() == 1) return node;
        int index = 0;
        for(;index < inorder.size();index++) {
            if(inorder[index] == postorder[postorder.size() - 1]) break;
        }
        vector<int> inorderleft(inorder.begin(), inorder.begin() + index);
        vector<int> inorderright(inorder.begin() + index + 1, inorder.end());
        postorder.resize(postorder.size() - 1);
        vector<int> postorderleft(postorder.begin(), postorder.begin() + inorderleft.size());
        vector<int> postorderright(postorder.begin() + inorderleft.size(), postorder.end());
        node->left = buildTree(inorderleft, postorderleft);
        node->right = buildTree(inorderright, postorderright);
        return node;
    }
};

第二道题和上一道题代码一样即可:

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size() == 0) return NULL;
        TreeNode* node = new TreeNode(preorder[0]);
        if(preorder.size() == 1) return node;
        int index = 0;
        for(;index < inorder.size();index++) {
            if(inorder[index] == preorder[0]) break;
        }
        vector<int> inorderleft(inorder.begin(), inorder.begin() + index);
        vector<int> inorderright(inorder.begin() + index + 1, inorder.end());
        preorder.erase(preorder.begin());
        vector<int> preorderleft(preorder.begin(), preorder.begin() + inorderleft.size());
        vector<int> preorderright(preorder.begin() + inorderleft.size(), preorder.end());
        node->left = buildTree(preorderleft, inorderleft);
        node->right = buildTree(preorderright, inorderright);
        return node;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值