(Python)

系统 1587 0

题目描述

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).

The binary search tree is guaranteed to have unique values.

Example 1:

Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
Output: 32

Example 2:

Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
Output: 23

Note:

The number of nodes in the tree is at most 10000.
The final answer is guaranteed to be less than 2^31.

            
              
                # Definition for a binary tree node.
              
              
                # class TreeNode(object):
              
              
                #     def __init__(self, x):
              
              
                #         self.val = x
              
              
                #         self.left = None
              
              
                #         self.right = None
              
              
                class
              
              
                Solution
              
              
                (
              
              
                object
              
              
                )
              
              
                :
              
              
                def
              
              
                rangeSumBST
              
              
                (
              
              self
              
                ,
              
               root
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
                :
              
              
                """
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: int
        """
              
              
                if
              
              
                not
              
               root
              
                :
              
              
                return
              
              
                0
              
              
        res 
              
                =
              
              
                0
              
              
                if
              
               L 
              
                <=
              
               root
              
                .
              
              val 
              
                <=
              
               R
              
                :
              
              
            res 
              
                +=
              
               root
              
                .
              
              val
            res 
              
                +=
              
               self
              
                .
              
              rangeSumBST
              
                (
              
              root
              
                .
              
              left
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
            res 
              
                +=
              
               self
              
                .
              
              rangeSumBST
              
                (
              
              root
              
                .
              
              right
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
                elif
              
               root
              
                .
              
              val 
              
                <
              
               L
              
                :
              
              
            res 
              
                +=
              
               self
              
                .
              
              rangeSumBST
              
                (
              
              root
              
                .
              
              right
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
                elif
              
               root
              
                .
              
              val 
              
                >
              
               R
              
                :
              
              
            res 
              
                +=
              
               self
              
                .
              
              rangeSumBST
              
                (
              
              root
              
                .
              
              left
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
                return
              
               res

            
          

简化代码:直接判断寻找的方向。如果root节点小于R,说明右边可以继续搜索;如果root节点大于L,说明左边可以继续搜索。

            
              
                # Definition for a binary tree node.
              
              
                # class TreeNode(object):
              
              
                #     def __init__(self, x):
              
              
                #         self.val = x
              
              
                #         self.left = None
              
              
                #         self.right = None
              
              
                class
              
              
                Solution
              
              
                (
              
              
                object
              
              
                )
              
              
                :
              
              
                def
              
              
                rangeSumBST
              
              
                (
              
              self
              
                ,
              
               root
              
                ,
              
               L
              
                ,
              
               R
              
                )
              
              
                :
              
              
                """
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: int
        """
              
              
        res 
              
                =
              
              
                [
              
              
                0
              
              
                ]
              
              
        self
              
                .
              
              dfs
              
                (
              
              root
              
                ,
              
               L
              
                ,
              
               R
              
                ,
              
               res
              
                )
              
              
                return
              
               res
              
                [
              
              
                0
              
              
                ]
              
              
                def
              
              
                dfs
              
              
                (
              
              self
              
                ,
              
               root
              
                ,
              
               L
              
                ,
              
               R
              
                ,
              
               res
              
                )
              
              
                :
              
              
                if
              
              
                not
              
               root
              
                :
              
              
                return
              
              
                if
              
               L 
              
                <=
              
               root
              
                .
              
              val 
              
                <=
              
               R
              
                :
              
              
            res
              
                [
              
              
                0
              
              
                ]
              
              
                +=
              
               root
              
                .
              
              val
        
              
                if
              
               root
              
                .
              
              val 
              
                <
              
               R
              
                :
              
              
            self
              
                .
              
              dfs
              
                (
              
              root
              
                .
              
              right
              
                ,
              
               L
              
                ,
              
               R
              
                ,
              
               res
              
                )
              
              
                if
              
               root
              
                .
              
              val 
              
                >
              
               L
              
                :
              
              
            self
              
                .
              
              dfs
              
                (
              
              root
              
                .
              
              left
              
                ,
              
               L
              
                ,
              
               R
              
                ,
              
               res
              
                )
              
            
          

参考来源:https://blog.csdn.net/fuxuemingzhu/article/details/83961263


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论