Skip to content

Commit c5f5d9a

Browse files
authored
Merge pull request #1020 from abckhush/main
Added LC solution 94 & 96
2 parents cb96dcd + 49f000b commit c5f5d9a

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
id: binary-tree-inorder-traversal
3+
title: Binary Tree Inorder Traversal
4+
difficulty: Easy
5+
sidebar_label: 0094-Binary Tree Inorder Traversal
6+
tags:
7+
- Binary Tree
8+
- Inorder
9+
- Traversal
10+
- LeetCode Easy
11+
---
12+
13+
## Problem
14+
15+
| Problem Statement | Solution Link | LeetCode Profile |
16+
| :---------------- | :------------ | :--------------- |
17+
| [Binary Tree Inorder Traversal ](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Binary Tree Inorder Traversal Solution on LeetCode](https://leetcode.com/problems/solutions/) | [Khushi Kalra](https://leetcode.com/u/abckhush/) |
18+
19+
## Problem Description
20+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
21+
### Example
22+
**Example 1:**
23+
```plaintext
24+
Input: root = [1,null,2,3]
25+
Output: [1,3,2]
26+
```
27+
**Example 2:**
28+
```plaintext
29+
Input: root = []
30+
Output: []
31+
```
32+
**Example 3:**
33+
```plaintext
34+
Input: root = [1]
35+
Output: [1]
36+
```
37+
38+
### Constraints
39+
1. The number of nodes in the tree is in the range `[0, 100]`.
40+
2. `-100 <= Node.val <= 100`
41+
42+
## Approach: Recursion
43+
The problem is to perform an inorder traversal of a binary tree and return the values in a list. An inorder traversal visits the nodes of the binary tree in the following order: left subtree, root node, right subtree.
44+
45+
We use a helper function traversal to perform the inorder traversal recursively. The main function inorderTraversal initializes the result list and calls the helper function.
46+
47+
1. Initialization:
48+
- Create an empty vector inOrder to store the result of the inorder traversal.
49+
50+
2. Recursive Traversal:
51+
- Define a helper function traversal that takes a TreeNode* and a reference to the inOrder vector.
52+
- If the current node (root) is NULL, return immediately (base case).
53+
- Recursively call traversal on the left subtree (root->left).
54+
- Push the value of the current node (root->val) to the inOrder vector.
55+
- Recursively call traversal on the right subtree (root->right).
56+
57+
3. Main Function:
58+
- In the inorderTraversal function, create an empty vector inOrder.
59+
- Call the traversal function with the root of the binary tree and the inOrder vector.
60+
- Return the inOrder vector containing the inorder traversal result.
61+
62+
## Complexity
63+
1. Time Complexity : $$O(h)$$
64+
2. Space Complexity : $$O(n)$$
65+
66+
## Code
67+
68+
### C++
69+
```cpp
70+
/**
71+
* Definition for a binary tree node.
72+
* struct TreeNode {
73+
* int val;
74+
* TreeNode *left;
75+
* TreeNode *right;
76+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
77+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
78+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
79+
* };
80+
*/
81+
class Solution {
82+
public:
83+
void traversal(TreeNode* root, vector<int>&inOrder){
84+
if(root==NULL) return;
85+
traversal(root->left, inOrder);
86+
inOrder.push_back(root->val);
87+
traversal(root->right, inOrder);
88+
}
89+
vector<int> inorderTraversal(TreeNode* root) {
90+
vector<int>inOrder;
91+
traversal(root, inOrder);
92+
return inOrder;
93+
}
94+
};
95+
```
96+
97+
### Python
98+
```python
99+
# Definition for a binary tree node.
100+
class TreeNode:
101+
def __init__(self, val=0, left=None, right=None):
102+
self.val = val
103+
self.left = left
104+
self.right = right
105+
106+
class Solution:
107+
def traversal(self, root, inOrder):
108+
if root is None:
109+
return
110+
self.traversal(root.left, inOrder)
111+
inOrder.append(root.val)
112+
self.traversal(root.right, inOrder)
113+
114+
def inorderTraversal(self, root):
115+
inOrder = []
116+
self.traversal(root, inOrder)
117+
return inOrder
118+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
id: unique-binary-search-tree
3+
title: Unique Binary Search Tree
4+
difficulty: Medium
5+
sidebar_label: 0096- Unique Binary Search Tree
6+
tags:
7+
- Binary Search Tree
8+
- LeetCode Medium
9+
---
10+
11+
## Problem
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [Unique Binary Search Tree ](https://leetcode.com/problems/unique-binary-search-trees/description/) | [Unique Binary Search Tree Solution on LeetCode](https://leetcode.com/problems/unique-binary-search-trees/solutions/4945144/easy-recursion-dynamic-programming-c-solution-beats-100) | [Khushi Kalra](https://leetcode.com/u/abckhush/) |
16+
17+
## Problem Description
18+
Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.
19+
20+
### Examples
21+
**Example 1:**
22+
```plaintext
23+
Input: n = 3
24+
Output: 5
25+
```
26+
**Example 2:**
27+
```plaintext
28+
Input: n = 1
29+
Output: 1
30+
```
31+
32+
### Constraints:
33+
`1 <= n <= 19`
34+
35+
## Approach#1 : RECURSION
36+
We use a recursive function numTrees(n) where numTrees(n) returns the number of unique BSTs that can be formed with n nodes. The following steps outline the approach:
37+
38+
1. Base Cases:
39+
If n is 0 or 1, return 1. There is exactly one unique BST that can be formed with 0 or 1 nodes:
40+
- numTrees(0) = 1: An empty tree is considered one unique BST.
41+
- numTrees(1) = 1: A single-node tree is also one unique BST.
42+
43+
2. Recursive Calculation:
44+
- Initialize count to 0. This variable will accumulate the total number of unique BSTs for n nodes.
45+
- For each i from 1 to n, consider i as the root of the BST:
46+
- The left subtree will have i-1 nodes.
47+
- The right subtree will have n-i nodes.
48+
- The total number of unique BSTs with i as the root is the product of the number of unique BSTs for the left and right subtrees.
49+
- Accumulate the results in count by adding the product of the number of unique BSTs for the left and right subtrees:
50+
`count += numTrees(i-1) * numTrees(n-i)`
51+
52+
3. Return the Result:
53+
- After computing the total count for all possible roots from 1 to n, return count.
54+
55+
### Complexity
56+
1. Time complexity: $$O(2^n)$$
57+
2. Space complexity: $$O(n)$$
58+
59+
### Code
60+
```cpp
61+
class Solution {
62+
public:
63+
int numTrees(int n) {
64+
if(n==0 || n==1) return 1;
65+
int count=0;
66+
for(int i=1; i<=n; i++){
67+
count= count + (numTrees(i-1)*numTrees(n-i));
68+
}
69+
return count;
70+
}
71+
};
72+
```
73+
74+
## Approach#2: DYNAMIC PROGRAMMING
75+
We use a dynamic programming array dp where dp[i] represents the number of unique BSTs that can be formed with i nodes. The following steps outline the approach:
76+
77+
1. Initialization:
78+
- Create a dynamic programming array dp of size n+1 initialized with zeros.
79+
- Set the base cases:
80+
- dp[0] = 1: An empty tree is considered one unique BST.
81+
- dp[1] = 1: A single-node tree is also one unique BST.
82+
83+
2. Filling the DP Array:
84+
- Iterate over the number of nodes from 2 to n (let's call this i).
85+
- For each i, consider each node j (from 1 to i) as the root of the BST.
86+
- The number of unique BSTs with i nodes can be computed as the sum of all possible left and right subtrees:
87+
- The left subtree will have j-1 nodes.
88+
- The right subtree will have i-j nodes.
89+
- Update the dp[i] value as the sum of the products of the number of unique BSTs for the left and right subtrees:
90+
`dp[i] += dp[j-1] * dp[i-j]`
91+
92+
3. Return the Result:
93+
- After filling the dp array, dp[n] will contain the number of unique BSTs that can be formed with n nodes.
94+
95+
### Complexity
96+
1. Time complexity: $$O(n^2)$$
97+
2. Space complexity: $$O(n)$$
98+
99+
### Code
100+
```cpp
101+
class Solution {
102+
public:
103+
int numTrees(int n) {
104+
vector<int>dp(n+1, 0);
105+
dp[0]=1;
106+
dp[1]=1;
107+
for(int i=2; i<=n; i++){
108+
for(int j=1; j<=i; j++){
109+
dp[i]= dp[i]+ dp[i-j]*dp[j-1];
110+
}
111+
}
112+
return dp[n];
113+
}
114+
};
115+
```

0 commit comments

Comments
 (0)