BST - 22: Construct Balanced BST from given values | Sorted Array to Balanced BST

  Рет қаралды 6,067

Coding Simplified

Coding Simplified

Күн бұрын

Source Code: thecodingsimpl...
Solution
Sort the given array
Now take variable start, end, mid
Get middle element & create node using value present at this index
Now recursively call value from start to mid - 1 in left side & mid + 1 to end in right side
At last return the node
Time Complexity: O(nlogn), if array is sorted then Time complexity will be O(n)
Space Complexity: O(1)
For more info, please see the video.
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my KZbin channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 500+ videos.
★☆★ SUBSCRIBE TO ME ON KZbin: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com

Пікірлер: 13
@UdhyaKumar-z4d
@UdhyaKumar-z4d 3 ай бұрын
nice thank you sir
@harshitrathi3077
@harshitrathi3077 4 жыл бұрын
Great Teaching !! Keep On Uploading.............
@CodingSimplified
@CodingSimplified 4 жыл бұрын
Thanks for your nice feedback. Keep Watching.
@adminuser1578
@adminuser1578 4 жыл бұрын
Thanks for video
@CodingSimplified
@CodingSimplified 4 жыл бұрын
Glad you liked it. Keep Watching.
@jacobkn6594
@jacobkn6594 3 жыл бұрын
Does it mean that it’s not possible to construct BBST from unsorted array?
@ozanservet
@ozanservet 2 жыл бұрын
It is accurate quick or merge sort is O(nlogn) but how inorder method can be O(n) I didn't understand. In addition, I don't think space complexity of this solution is O(1). It uses recursive. Why we do not count space complexity of recursive because of stacks? My solution is below, I am open to feedbacks; public Node convertBstToBalancedTree(Node node) { List list = new ArrayList(); inorderList(node, list); return makeBalanced(createNewNode(-1), list); } public Node makeBalanced(Node node, List inordered) { if(inordered.size() == 0) { return null; } int halfSize = inordered.size() / 2; if(inordered.size() % 2 == 1) { halfSize = (inordered.size() - 1) / 2; } node = createNewNode(inordered.get(halfSize)); node.left = makeBalanced(node.left, inordered.subList(0, halfSize)); node.right = makeBalanced(node.right, inordered.subList(halfSize + 1, inordered.size())); return node; }
@techamet428
@techamet428 4 жыл бұрын
good explanation..keep it up
@CodingSimplified
@CodingSimplified 4 жыл бұрын
Thanks for your nice feedback. Keep Watching.
@abhinavagrawal6148
@abhinavagrawal6148 4 жыл бұрын
Hi Coding Simplified. Please tell what keyboard shortcuts you use while debugging your code. Please reply.
@CodingSimplified
@CodingSimplified 4 жыл бұрын
They're simply Eclipse IDE shortcut. Like while debugging in Eclipse. You'll use F6 to execute a line & F5 to go inside a function. For more info, please see eclipse debug shortcut on google.
@shouvikdatta6831
@shouvikdatta6831 4 жыл бұрын
How the space complexity be O(1)? We are making a BST of size n... So, I think it would be O(n)
@CodingSimplified
@CodingSimplified 4 жыл бұрын
That's right. If you consider node of tree as well then yes It's O(n). Apart than this, we're not using any other extra space.
BST - 23: Get Inorder Predecessor for a given value in BST
15:07
Coding Simplified
Рет қаралды 4,2 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Рет қаралды 385 М.
What Is a Binary Heap?
8:45
Spanning Tree
Рет қаралды 209 М.
The 7 Levels of Math Symbols
14:03
The Unqualified Tutor
Рет қаралды 75 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 47 МЛН
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Codebagel
Рет қаралды 262 М.
Binary Trees - Data Structures Explained
10:18
Aaron Jack
Рет қаралды 152 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН