Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the range and sum of binary search tree by LeetCode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve the scope of LeetCode binary search tree and, I hope you will learn something after reading this article, let's discuss it together!

Topic description

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

The binary search tree is guaranteed to have unique values.

Example 1:

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

Example 2:

Input: root = [10, 5, 15, 15, 7, 13, 18, 18, 1, 6, R = 10 output: 23

Tip:

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

-A witty line of thinking-

-A witty line of thinking-

-A witty line of thinking-

The idea of solving the problem

Tags: depth first traversal

Meaning: the literal meaning of this question is difficult to understand. The original intention is to find out the sum of all X > = L and X R, then return the sum of the left subtrees.

Current node X > = L and X R) {return rangeSumBST (root.left, L, R);} return root.val + rangeSumBST (root.left, L, R) + rangeSumBST (root.right, L, R) }} after reading this article, I believe you have a certain understanding of "how to realize the scope of binary search tree in LeetCode". If you want to know more about it, please follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report