欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

(Python)

系統 1607 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條評論
主站蜘蛛池模板: 中国一级特黄 | 欧美—级v免费大片 | 精品国产免费一区二区三区 | 碰碰碰人人澡人人爱摸 | 91免费国产在线 | 欧美成人观看 | 91精品国产一区二区三区蜜臀 | 亚洲www啪成人一区二区 | 亚洲综合区| 成人午夜久久 | 奇米影视亚洲春色 | 亚洲国产精品久久婷婷 | 粉色视频高清大全免费观看1 | 欧美精品日韩 | 天天舔天天舔 | 国产综合精品久久亚洲 | 欧美一区二区三区成人精品 | 第一页在线视频 | 91精品国产综合久久青草 | 色黄视频免费观看 | 天天摸天天爽天天澡视频 | 欧美一级视频在线观看欧美 | 中文字幕精品一区 | www.久久草| 午夜影院在线观看 | 日韩精品视频在线播放 | 欧美经典剧情系列h版在线观看 | 中文字幕免费 | 欧美一区二区在线视频 | 99re99 | 亚洲精品在线看 | 亚洲欧美天堂综合久久 | 国产精品亚洲综合 | 日本免费成人 | 久久55| 欧美日韩精品一区二区在线线 | 国产精品一区二区三区99 | 91精品啪国产在线观看免费牛牛 | 97国产精品最新 | 亚洲精品欧美一区二区三区 | 日韩久久精品 |