Skip to content

Commit 050e719

Browse files
authored
Merge pull request #913 from Anshika14528/226
Added leetcode problem:226
2 parents 781e9e9 + 1aadb6f commit 050e719

File tree

4 files changed

+257
-1
lines changed

4 files changed

+257
-1
lines changed

dsa-solutions/lc-solutions/0000-0099/0017-letter-combinations-of-a-phone-number.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations
313313
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
314314
- Return the list of combinations.
315315

316-
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
316+
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
id: validate-binary-search-tree
3+
title: Validate Binary Search Tree
4+
sidebar_label: 0098 Validate Binary Search Tree
5+
tags:
6+
- tree
7+
- tree traversal
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Validate Binary Search Tree problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
16+
17+
A valid BST is defined as follows:
18+
19+
* The left subtree of a node contains only nodes with keys less than the node's key.
20+
* The right subtree of a node contains only nodes with keys greater than the node's key.
21+
* Both the left and right subtrees must also be binary search trees.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
```
28+
29+
Input: root = [2,1,3]
30+
Output: true
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: root = [5,1,4,null,null,3,6]
37+
Output: false
38+
```
39+
40+
### Constraints
41+
42+
- The number of nodes in the tree is in the range $[1, 10^4]$.
43+
- $-2^(31) \leq \text{Node.val} \leq 2^(31) - 1$.
44+
45+
### Approach
46+
47+
To solve this problem(valid BST) we will do the inorder traversal of the tree as we know in a binary search tree the array of elements which we get after inorder traversal they will be in sorted order so after the inorder traversal we will just have check if the inorder traversal of the given binary search is in sorted order or not.
48+
49+
#### Code in C++
50+
51+
```cpp
52+
/**
53+
* Definition for a binary tree node.
54+
* struct TreeNode {
55+
* int val;
56+
* TreeNode *left;
57+
* TreeNode *right;
58+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
59+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
60+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
61+
* right(right) {}
62+
* };
63+
*/
64+
class Solution {
65+
public:
66+
// Function for inorder traversal
67+
void inorder(TreeNode* root , vector<int>&a){
68+
if(root==NULL){
69+
return;
70+
}
71+
inorder(root->left,a);
72+
a.push_back(root->val);
73+
inorder(root->right,a);
74+
}
75+
bool isValidBST(TreeNode* root) {
76+
vector<int>a,b;
77+
inorder(root,a);
78+
b=a;
79+
for(int i=1;i<a.size();i++){
80+
if(a[i]==a[i-1]){ // it helps us to check that elements in given tree is not equal for valid BST
81+
return false;
82+
}
83+
}
84+
sort(b.begin(),b.end());
85+
for(int i=0;i<b.size();i++){
86+
if(b[i]!=a[i]){
87+
return false;
88+
}
89+
}
90+
return true;
91+
}
92+
};
93+
```
94+
95+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: invert-binary-search-tree
3+
title: Invert Binary Search Tree
4+
sidebar_label: 0226 Invert Binary Search Tree
5+
tags:
6+
- tree
7+
- recursion
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Invert Binary Search Tree problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
Given the root of a binary tree, invert the tree, and return its root.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
23+
Input: root = [4,2,7,1,3,6,9]
24+
Output: [4,7,2,9,6,3,1]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: root = [2,1,3]
31+
Output: [2,3,1]
32+
```
33+
34+
```
35+
Input: root = []
36+
Output: []
37+
```
38+
39+
### Constraints
40+
41+
- The number of nodes in the tree is in the range $[0, 100]$.
42+
- $-100 \leq \text{Node.val} \leq 100$
43+
44+
### Approach
45+
46+
To solve this problem(invert BST) we will use recursion(call function repeatedly) and temporary tree node(t) for swapping.
47+
48+
#### Code in C++
49+
50+
```cpp
51+
/**
52+
* Definition for a binary tree node.
53+
* struct TreeNode {
54+
* int val;
55+
* TreeNode *left;
56+
* TreeNode *right;
57+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
58+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
59+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
60+
* };
61+
*/
62+
class Solution {
63+
public:
64+
void invert(TreeNode* root){
65+
if(root==NULL){
66+
return;
67+
}
68+
TreeNode* t;
69+
t=root->right;
70+
root->right=root->left;
71+
root->left=t;
72+
invert(root->left);
73+
invert(root->right);
74+
}
75+
TreeNode* invertTree(TreeNode* root) {
76+
invert(root);
77+
return root;
78+
79+
}
80+
};
81+
```
82+
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
id: kth-smallest-element-in-binary-search-tree
3+
title: Kth Smallest Element in Binary Search Tree
4+
sidebar_label: 0230 Kth Smallest Element in Binary Search Tree
5+
tags:
6+
- tree
7+
- tree traversal
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to theKth Smallest Element in Binary Search Tree problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
23+
Input: root = [3,1,4,null,2], k = 1
24+
Output: 1
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: root = [5,3,6,2,4,null,null,1], k = 3
31+
Output: 3
32+
```
33+
34+
### Constraints
35+
36+
- The number of nodes in the tree is n.
37+
- $1 \leq k \leq n \leq 10^4$
38+
- $0 \leq \text{Node.val} \leq 10^4$
39+
40+
### Approach
41+
42+
To solve this problem(Kth smallest element in BST) we will do the inorder traversal of the tree as we know in a binary search tree the array of elements which we get after inorder traversal they will be in sorted order so the kth smallest element in the given BST will be the kth index(1-indexed) element in inorder traversal of the BST.
43+
44+
#### Code in C++
45+
46+
```cpp
47+
/**
48+
* Definition for a binary tree node.
49+
* struct TreeNode {
50+
* int val;
51+
* TreeNode *left;
52+
* TreeNode *right;
53+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
54+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
55+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
56+
* };
57+
*/
58+
class Solution {
59+
public:
60+
// Function for inorder traversal
61+
void inorder(TreeNode* root,vector<int>&a){
62+
if(root==NULL){
63+
return;
64+
}
65+
inorder(root->left,a);
66+
a.push_back(root->val);
67+
inorder(root->right,a);
68+
}
69+
// Function to get the kth smallest element
70+
int kthSmallest(TreeNode* root, int k) {
71+
vector<int>a;
72+
inorder(root,a);
73+
return a[k-1]; // as 0-indexed
74+
}
75+
};
76+
```
77+
78+

0 commit comments

Comments
 (0)