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

Sdílet
Vložit
  • čas přidán 12. 09. 2020
  • Source Code: thecodingsimplified.com/conve...
    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: ★☆★
    thecodingsimplified.com
    I started my CZcams channel, Coding Simplified, during Dec of 2015.
    Since then, I've published over 500+ videos.
    ★☆★ SUBSCRIBE TO ME ON CZcams: ★☆★
    czcams.com/users/codingsimplif...
    ★☆★ Send us mail at: ★☆★
    Email: thecodingsimplified@gmail.com

Komentáře • 7

  • @lokeshkumarrathinavel6566

    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.

  • @avikdutta5965
    @avikdutta5965 Před 2 lety +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]

  • @harish-wi3ts
    @harish-wi3ts Před 3 lety +1

    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  Před 3 lety +2

      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 Před 3 lety

    sir u missed that headlist .left==null

  • @ozanservet
    @ozanservet Před rokem

    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();
    }