【leetcode】二叉搜索树的范围和

 

int rangeSumBST(struct TreeNode* root, int L, int R){
    if (!root) return 0;
    if (root->val < L) return rangeSumBST(root->right,L,R);
    if (root->val > R) return rangeSumBST(root->left,L,R);
    return root->val + rangeSumBST(root->left,L,R) + rangeSumBST(root->right,L,R);
}

 

posted @ 2020-09-10 15:47  温暖了寂寞  阅读(121)  评论(0编辑  收藏  举报