Skip to content

Commit 2600b45

Browse files
authored
Merge pull request #975 from Anshika14528/109
Added Leetcode Problem:109 #955
2 parents 9c198ed + 7dec1d0 commit 2600b45

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
id: convert-sorted-list-to-binary-search-tree
3+
title: Convert Sorted List to Binary Search Tree
4+
sidebar_label: 0109 Convert Sorted List to Binary Search Tree
5+
tags:
6+
- tree
7+
- tree traversal
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Convert Sorted List to Binary Search Tree problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced
16+
binary search tree.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
24+
Input: head = [-10,-3,0,5,9]
25+
Output: [0,-3,9,-10,null,5]
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: head = []
32+
Output: []
33+
```
34+
35+
36+
37+
### Constraints
38+
39+
- The number of nodes in head is in the range $[0, 2 * 10^4]$.
40+
- $-10^5 \leq \text{Node.val} \leq 10^5$.
41+
42+
### Approach
43+
44+
To solve this problem(recover BST) as we know that the inorder traversal of binary search tree give us sorted array so when we traverse the array from left to right we will get to know that at some node the previous value will be greater than current value and that will be our first point were order is disturbed store current and its previous node so that we can counter if its an adjacent swap and if the order is disturbed for the second time, just store that node.After all this just swap the first and last value as you can in the below given code.
45+
46+
#### Code in C++
47+
48+
```cpp
49+
/**
50+
* Definition for singly-linked list.
51+
* struct ListNode {
52+
* int val;
53+
* ListNode *next;
54+
* ListNode() : val(0), next(nullptr) {}
55+
* ListNode(int x) : val(x), next(nullptr) {}
56+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
57+
* };
58+
*/
59+
/**
60+
* Definition for a binary tree node.
61+
* struct TreeNode {
62+
* int val;
63+
* TreeNode *left;
64+
* TreeNode *right;
65+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
66+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
67+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
68+
* };
69+
*/
70+
class Solution {
71+
public:
72+
TreeNode* tree(vector<int>&a){
73+
if(a.size()==0){
74+
return NULL;
75+
}
76+
vector<int>q,w;
77+
for(int i=0;i<a.size()/2;i++){
78+
q.push_back(a[i]);
79+
}
80+
for(int i=(a.size()/2)+1 ;i<a.size();i++){
81+
w.push_back(a[i]);
82+
}
83+
TreeNode* p =new TreeNode(a[a.size()/2]);
84+
p->left=tree(q);
85+
p->right=tree(w);
86+
return p;
87+
}
88+
TreeNode* sortedListToBST(ListNode* head) {
89+
vector<int>a;
90+
while(head!=NULL){
91+
a.push_back(head->val);
92+
head=head->next;
93+
}
94+
return tree(a);
95+
96+
}
97+
};
98+
```
99+
100+

0 commit comments

Comments
 (0)