filmov
tv
BST - 14: Check if there exists a pair for given Sum in BST | Using Set | Using Inorder
![preview_player](https://i.ytimg.com/vi/zSg6yfLduro/maxresdefault.jpg)
Показать описание
Solution - 1
- Traverse BST in preorder manner
- Initialize a Haseset
- If it present, return true, else add current node value in set
- Traverse every node in tree & check above
Time Complexity: O(n)
Space Complexity: O(n)
Solution - 2
- Traverse BST in inorder
- While Traversing in inorder, keepon adding value as well in list as well
- After traversing, we'll have sorted array/list
- Now take two variable start & end, start will point to o index & end will point to last index
- Get sum of values present at start & end index
- If CurrentSum is equal to given sum, return true.
- If CurrentSum is less than given sum, increase start by 1.
- If CurrentSum is greater than given sum, decrese the end by 1
Time Complexity: O(n)
Space Complexity: O(n)
For more info, please see the video.
CHECK OUT CODING SIMPLIFIED
★☆★ VIEW THE BLOG POST: ★☆★
I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 500+ videos.
★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★
★☆★ Send us mail at: ★☆★
- Traverse BST in preorder manner
- Initialize a Haseset
- If it present, return true, else add current node value in set
- Traverse every node in tree & check above
Time Complexity: O(n)
Space Complexity: O(n)
Solution - 2
- Traverse BST in inorder
- While Traversing in inorder, keepon adding value as well in list as well
- After traversing, we'll have sorted array/list
- Now take two variable start & end, start will point to o index & end will point to last index
- Get sum of values present at start & end index
- If CurrentSum is equal to given sum, return true.
- If CurrentSum is less than given sum, increase start by 1.
- If CurrentSum is greater than given sum, decrese the end by 1
Time Complexity: O(n)
Space Complexity: O(n)
For more info, please see the video.
CHECK OUT CODING SIMPLIFIED
★☆★ VIEW THE BLOG POST: ★☆★
I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 500+ videos.
★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★
★☆★ Send us mail at: ★☆★
BST - 14: Check if there exists a pair for given Sum in BST | Using Set | Using Inorder
BST - 15: Check if there exists a Triplet for given Sum in BST
BST - 20: Check if given Binary Tree is BST
Check for BST | Validate Binary Search Tree | Validate BST | Check BST | DSA-One Course #69
L51. Two Sum In BST | Check if there exists a pair with Sum K
2-sum BST | Find a pair with given sum in a BST | 2 Methods
BST - 19: Check if elements of two BST are same
Check if a tree is a BST or not | Binary Search Tree | Love Babbar DSA Sheet | Amazon 🔥
Check if Binary Tree is Binary Search Tree | Validate BST
Shortest Range in BST GeeksForGeeks (Hard)
Validate Binary Search Tree | Check If a Binary Tree is BST or Not | Java
Check for BST
Check for BST or Not || Program 37 || Competitive Coding || Learning Monkey ||
Lecture 39 || Check if all internal nodes of a BST have single child node
Check if a Binary Tree is a BST or Not | BST | FavTutor
(BST) Check if a given Binary Tree is BST or not? || Binary Search Trees
Search a node in BST | School Practice Problem | GeeksforGeeks School
2.Check for BST | Tree | Python | GFG | Must Do Coding Questions
Java method to check if a binary tree is a valid BST (2 Solutions!!)
Two Sum In BST | Check if there exists a pair with Sum K| Data Structure And Algorithm|#programming
Check for BST (A Sample Video for DSA-Self Paced Course) | GeeksforGeeks
Print BST keys in the given range | GeeksforGeeks
L40. Search in a Binary Search Tree | BST | C++ | Java
This is the Polestar 2 BST and you can’t buy it. 🙂
Комментарии