当前位置:网站首页>LeetCode 938. Range sum of binary search tree
LeetCode 938. Range sum of binary search tree
2022-06-24 03:40:00 【freesan44】
Title address (938. The range of binary search tree and )
https://leetcode-cn.com/problems/range-sum-of-bst/
Title Description
Given the root of the binary search tree root, The return value is in the range [low, high] The sum of the values of all nodes between . Example 1: Input :root = [10,5,15,3,7,null,18], low = 7, high = 15 Output :32 Example 2: Input :root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10 Output :23 Tips : The number of nodes in the tree is in the range [1, 2 * 104] Inside 1 <= Node.val <= 105 1 <= low <= high <= 105 all Node.val Different from each other
Ideas
DFS Traverse
Code
- Language support :Python3
Python3 Code:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
self.res = 0# Set public variables
def dfs(node:TreeNode):
if low <= node.val <= high:
# print(node.val)
self.res += node.val
if node.left:
dfs(node.left)
if node.right:
dfs(node.right)
dfs(root)
return self.resComplexity analysis
Make n Is array length .
- Time complexity :$O(n)$
- Spatial complexity :$O(1)$
边栏推荐
- What is elastic scaling in cloud computing? What are the main applications of elastic scaling in cloud computing?
- Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
- What is the edge calculator force? What about the edge calculator?
- Mocktio usage (Part 2)
- MySQL stored procedure + function
- [Numpy] Numpy对于NaN值的判断
- Get to know MySQL database
- [competition experience sharing] design of intelligent guide rod
- What is load balancing? What are the functions of load balancing?
- RPM 包的构建 - SPEC 基础知识
猜你喜欢

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?

Halcon knowledge: contour operator on region (2)

老弹出explorer.exe遇到问题已停止工作,怎么办?

Ar 3D map technology

On Sunday, I rolled up the uni app "uview excellent UI framework"

ClickHouse(02)ClickHouse架构设计介绍概述与ClickHouse数据分片设计

在pycharm中pytorch的安装

内存泄漏之KOOM

Old popup explorer Exe has stopped working due to problems. What should I do?
![[Numpy] Numpy对于NaN值的判断](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[Numpy] Numpy对于NaN值的判断
随机推荐
An example of SPM manual binding execution plan
Use lightweight application server to automatically download and upload to onedrive
Web penetration test - 5. Brute force cracking vulnerability - (7) MySQL password cracking
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
How to install CentOS 6.5 PHP extension
Grpc: how to add API log interceptors / Middleware?
golang clean a slice
What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
Big coffee face to face | Dr. Chen Guoguo talks about intelligent voice
NLP task summary introduction and understanding
Differences between EDI and VMI
MySQL stored procedure + function
Why can't the fortress machine log in? What are the ways to solve the problem
[code Capriccio - dynamic planning] t392 Judgement subsequence
Coding Ci of Devops
开源一款监控数据采集器,啥都能监控
Rasa 3.x 学习系列-Rasa 3.2.0 新版本发布
The medical technology giant was blackmailed and Microsoft announced 74 security vulnerabilities | global network security hotspot
Event id:7001: after restarting the machine, the World Wide Web failed to start automatically, resulting in inaccessible websites
618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?