(C++二叉树04) 找左下角的值 路径总和I和II

513、找树左下角的值

递归:

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

层序遍历:

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if(root == NULL) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int result = 0;
        while(!que.empty()) {
            int size = que.size();
            for(int i = 0; i < size; i++) {
                TreeNode* tem = que.front();
                que.pop();
                if(tem->right != NULL) que.push(tem->right);
                if(tem->left != NULL) que.push(tem->left);
                if(i == size - 1) {
                    result = tem->val;
                }
            }
        }
        return result;
    }
};

112、路径总和

递归法:

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

113、路径总和 II

递归法:

class Solution {
public:
    vector<vector<int>> result;
    int target = 0;
    void traversal(TreeNode* root, vector<int> path, int pathSum) {
        if(root->left == NULL && root->right == NULL) {
            if(pathSum == target) {
                result.push_back(path);
            }
            return;
        }
        if(root->left != NULL) {
            path.push_back(root->left->val);
            traversal(root->left, path, pathSum + root->left->val);
            path.pop_back();
        }
        if(root->right != NULL) {
            path.push_back(root->right->val);
            traversal(root->right, path, pathSum + root->right->val);
            path.pop_back();
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(root == NULL) return result;
        vector<int> path;
        path.push_back(root->val);
        target = targetSum;
        traversal(root, path, root->val);
        return result;
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值