Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Convert Sorted List to Binary Search Tree problem solution

YASH PAL, 31 July 2024

In this Leetcode Convert Sorted List to Binary Search Tree problem solution we have Given the head of a singly linked list where elements are sorted in ascending order, convert to a height-balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than 1.

Leetcode Convert Sorted List to Binary Search Tree problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

def sortedListToBST(self, head):
        if not head: return None
        return self.DFS(head)
       
    def DFS(self, head):
	
        if not head: 
            return None

        if not head.next:
            return TreeNode(head.val)
        prev, slow, fast = None, head, head
        while fast and fast.next:
            prev = slow
            slow = slow.next
            fast = fast.next.next
            
        tmp = prev.next
        prev.next = None
        node = TreeNode(slow.val)
        node.left = self.DFS(head)
        node.right = self.DFS(slow.next)
        return node

Problem solution in Java.

class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        if(head==null)
            return null;
        else
            return BST(head,null);
    }
    public static TreeNode BST(ListNode head,ListNode tail){
        ListNode slow=head;
        ListNode fast=head;
        if(head==tail)
            return null;
        while(fast!=tail&&fast.next!=tail){
            slow=slow.next;
            fast=fast.next.next;
        }
        TreeNode root=new TreeNode(slow.val);
        root.left=BST(head,slow);
        root.right=BST(slow.next,tail);
        return root;
    }
}

Problem solution in C++.

class Solution {
public:
    
    
    TreeNode* sortedListToBST(ListNode* head) {
        if(!head)
            return NULL;
        ListNode *slow=head;
        ListNode *fast=head->next,*tt=NULL;
        while(fast and fast->next)
        {
            tt=slow;
            slow=slow->next;
            fast=fast->next->next;
        }
        if(tt)
            tt->next=NULL;
        else 
            head=NULL;
        
        TreeNode *curr=new TreeNode(slow->val);
        TreeNode *r=sortedListToBST(slow->next);
        TreeNode *l=sortedListToBST(head);
        curr->left=l;curr->right=r;
        return curr;
    }
};

Problem solution in C.

struct TreeNode* get(int v){
		struct TreeNode* x;
		x=(struct TreeNode*)malloc(sizeof(struct TreeNode));
		x->val=v;
		x->left=NULL;
		x->right=NULL;
		return x;
}
struct TreeNode* func(int *b,int l,int h){
		struct TreeNode* root;
		int m=(l+h)/2;   
		if(h>=l){
			root=get(b[m]);
			root->left=func(b,l,m-1);
			root->right=func(b,m+1,h);
			return root;
		}
		else
			return NULL;
}

struct TreeNode* sortedListToBST(struct ListNode* head){
		struct TreeNode* t;
		int a=pow(10,4);
		int b[2*a];
		int i=0,j;
		while(head!=NULL){
			b[i]=head->val;
			head=head->next;
			i++;
		}
		int l=0,h=i-1;
		t=func(b,l,h);
		return t;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes