您当前的位置: 首页 >  搜索

孑渡

暂无认证

  • 5浏览

    0关注

    178博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

每日一题2021/4/27:二叉搜索树的范围和

孑渡 发布时间:2021-04-27 15:18:17 ,浏览量:5

给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。

/**
 * 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:
    int rangeSumBST(TreeNode* root, int low, int high) {
        if(root == NULL){
            return 0;
        }else{
            int temp = 0;
            if(root->val >= low && root->val val;
            return temp + rangeSumBST(root->left, low, high) + rangeSumBST(root->right, low, high);
        }
    }
};
关注
打赏
1663211900
查看更多评论
立即登录/注册

微信扫码登录

0.2023s