Skip to content

Added leetcode problem:226 #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
- Return the list of combinations.

This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
id: validate-binary-search-tree
title: Validate Binary Search Tree
sidebar_label: 0098 Validate Binary Search Tree
tags:
- tree
- tree traversal
- LeetCode
- C++
description: "This is a solution to the Validate Binary Search Tree problem on LeetCode."
---

## Problem Description

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.

### Examples

**Example 1:**

```

Input: root = [2,1,3]
Output: true
```

**Example 2:**

```
Input: root = [5,1,4,null,null,3,6]
Output: false
```

### Constraints

- The number of nodes in the tree is in the range $[1, 10^4]$.
- $-2^(31) \leq \text{Node.val} \leq 2^(31) - 1$.

### Approach

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.

#### Code in C++

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
// Function for inorder traversal
void inorder(TreeNode* root , vector<int>&a){
if(root==NULL){
return;
}
inorder(root->left,a);
a.push_back(root->val);
inorder(root->right,a);
}
bool isValidBST(TreeNode* root) {
vector<int>a,b;
inorder(root,a);
b=a;
for(int i=1;i<a.size();i++){
if(a[i]==a[i-1]){ // it helps us to check that elements in given tree is not equal for valid BST
return false;
}
}
sort(b.begin(),b.end());
for(int i=0;i<b.size();i++){
if(b[i]!=a[i]){
return false;
}
}
return true;
}
};
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
id: invert-binary-search-tree
title: Invert Binary Search Tree
sidebar_label: 0226 Invert Binary Search Tree
tags:
- tree
- recursion
- LeetCode
- C++
description: "This is a solution to the Invert Binary Search Tree problem on LeetCode."
---

## Problem Description

Given the root of a binary tree, invert the tree, and return its root.

### Examples

**Example 1:**

```

Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
```

**Example 2:**

```
Input: root = [2,1,3]
Output: [2,3,1]
```

```
Input: root = []
Output: []
```

### Constraints

- The number of nodes in the tree is in the range $[0, 100]$.
- $-100 \leq \text{Node.val} \leq 100$

### Approach

To solve this problem(invert BST) we will use recursion(call function repeatedly) and temporary tree node(t) for swapping.

#### Code in C++

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void invert(TreeNode* root){
if(root==NULL){
return;
}
TreeNode* t;
t=root->right;
root->right=root->left;
root->left=t;
invert(root->left);
invert(root->right);
}
TreeNode* invertTree(TreeNode* root) {
invert(root);
return root;

}
};
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
id: kth-smallest-element-in-binary-search-tree
title: Kth Smallest Element in Binary Search Tree
sidebar_label: 0230 Kth Smallest Element in Binary Search Tree
tags:
- tree
- tree traversal
- LeetCode
- C++
description: "This is a solution to theKth Smallest Element in Binary Search Tree problem on LeetCode."
---

## Problem Description

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.

### Examples

**Example 1:**

```

Input: root = [3,1,4,null,2], k = 1
Output: 1
```

**Example 2:**

```
Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
```

### Constraints

- The number of nodes in the tree is n.
- $1 \leq k \leq n \leq 10^4$
- $0 \leq \text{Node.val} \leq 10^4$

### Approach

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.

#### Code in C++

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// Function for inorder traversal
void inorder(TreeNode* root,vector<int>&a){
if(root==NULL){
return;
}
inorder(root->left,a);
a.push_back(root->val);
inorder(root->right,a);
}
// Function to get the kth smallest element
int kthSmallest(TreeNode* root, int k) {
vector<int>a;
inorder(root,a);
return a[k-1]; // as 0-indexed
}
};
```


Loading