LeetCode - 938. Range Sum of BST | Day 14 December Challenge
Aditya Mahajan
Problem: https://leetcode.com/problems/range-sum-of-bst/ Code Link: https://github.com/skystone1000/LeetCode
December Challenge 2021 - Day 14 938. Range Sum of BST
TIMESTAMPS 0:00 Intro 0:19 Question & Examples 1:21 Approach 1 - Recursive 2:49 Approach 1 - Code 5:22 Approach 2 - Iterative - BFS 6:10 Approach 2 - Code 8:28 Output
Approach We need to calculate the sum of all values in a given range. Some of the approaches
- Recursive: The simplest way we can think of is to traverse every node and check if its value lies in the given range, and if it does then add it to the sum so our simple inOrder traversal could also do the work. Now the optimization we can think of is the property of BST that all nodes on the left side of the root are smaller and all nodes on the roots right are greater, so we can use that to add two checks to our recursive calls.
- Iterative BFS: A alternative way with a similar approach is with BFS or DFS so we will take a look at BFS. We use standard BFS tree traversal by adding root to queue and then check for each node for left and right nodes and if they are satisfying our checks of high and low with the BST property then we push back those nodes in the queue.
š Social Media š
š LinkedIn: https://www.linkedin.com/in/adityamahajan123/ š GitHub: https://github.com/skystone1000/ šø Instagram: https://www.instagram.com/skystone1000/ š Chess.com : https://www.chess.com/member/skystone1000 ā Discord Server: https://discord.gg/ZPWzT5QWDC
ā” Please leave a LIKE and SUBSCRIBE for more content! ā”
ā Tags ā
- Aditya Mahajan
ā Hashtags ā #leetcode #938Leetcode ... https://www.youtube.com/watch?v=_B2_rCX__9U
54144832 Bytes