BST - 21: Convert BST to Sorted Doubly Linked List (DLL)

  Рет қаралды 7,638

Coding Simplified

Coding Simplified

Күн бұрын

Source Code: thecodingsimpl...
Solution
- We traverse the binary tree in inorder manner
- We take a global variable 'prev' & 'headOfList'
- We assign lowest node as headOfList. This is when prev is null.
- When prev is not null, then node.left = prev & prev.right = node
- After each iteration we update the prev to current node
Time Complexity: 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

Пікірлер: 7
@avikdutta5965
@avikdutta5965 2 жыл бұрын
space complexity cannot be O(1) because there is recursion function for that we required auxiliary space. So space complexity is near about height of the tree, i.e. O(h) [h = height of the tree]
@lokeshkumarrathinavel6566
@lokeshkumarrathinavel6566 3 жыл бұрын
I liked your explanation... made it easy to understand. Actually I solved leet code - 426. Convert Binary Search Tree to Sorted Doubly Linked List - with this explanation. Everything same.. just make it circular, do this.. head.left = prev and prev.right = head. :) Appreciated!!!Thanks Man.
@CodingSimplified
@CodingSimplified 3 жыл бұрын
Thanks for your nice feedback.
@harish-wi3ts
@harish-wi3ts 4 жыл бұрын
Hi...sir I am unable to solve a single problem in leetcode What are the maths concepts are required...to solve as java developer.sir please do video on this.. or reply me sir. Thanks you.
@CodingSimplified
@CodingSimplified 4 жыл бұрын
Sure, I'll try to these questions. My suggestion is if you finding in leetcode some difficulty, don't focus much on it but start from basics first. For example, in our playlist we've basics to good questions. Once you've some command on a topic, you can see leetcode questions.
@saisiddana9345
@saisiddana9345 3 жыл бұрын
sir u missed that headlist .left==null
@ozanservet
@ozanservet 2 жыл бұрын
I think we should count recursive space complexity in the stack and it might be O(n). My solution is below, I am open to comments; Node dLL = null; public void convertBSTToSourtedDoublyLinkedList(Node node) { if(node == null) { return; } convertBSTToSourtedDoublyLinkedList(node.left); if(dLL == null) { dLL = new Node(node.getData()); } else { Node tmp = new Node(node.getData()); dLL.right = tmp; tmp.left = dLL; dLL = dLL.right; } convertBSTToSourtedDoublyLinkedList(node.right); } public void printdLL() { Node node = dLL; System.out.print("printdLL: "); while(node != null) { System.out.print(node.getData() + " "); node = node.left; } System.out.println(); }
when you have plan B 😂
00:11
Andrey Grechka
Рет қаралды 41 МЛН
Touching Act of Kindness Brings Hope to the Homeless #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 18 МЛН
ДОКАЗАЛ ЧТО НЕ КАБЛУК #shorts
00:30
Паша Осадчий
Рет қаралды 904 М.
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 85 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 9 М.
Flatten Binary Tree to Linked List - Leetcode 114 - Python
14:27
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 373 М.
What is Balancing a binary tree and why do we need balancing
25:39
Simple Snippets
Рет қаралды 35 М.
How to: Work at Google - Example Coding/Engineering Interview
24:02
Life at Google
Рет қаралды 7 МЛН
when you have plan B 😂
00:11
Andrey Grechka
Рет қаралды 41 МЛН