Skip to content

Commit 8bd610d

Browse files
committed
minor: fixed bug in README for Arrays
1 parent c4f2c52 commit 8bd610d

File tree

6 files changed

+152
-9
lines changed

6 files changed

+152
-9
lines changed

00_Arrays/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
12. Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. - **[best-time-to-buy-and-sell-stock](best-time-to-buy-and-sell-stock)**
2626

27-
13. Given an integer array nums, find the contiguous subarray within an array which has the largest product. *[maximum-product-subarray](maximum-product-subarray)**
27+
13. Given an integer array nums, find the contiguous subarray within an array which has the largest product. **[maximum-product-subarray](maximum-product-subarray)**
2828

2929
## Intervals
3030

04_Trees_and_Graphs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
15. Write a function to find the total number of leaf nodes in a binary tree. **[countTheLeaves](countTheLeaves)**
1919
16. Given a binary tree, find its maximum depth. **[maximum-depth-of-binary-tree](maximum-depth-of-binary-tree)**
2020
17. Invert Binary Tree **[invert-binary-tree](invert-binary-tree)**
21+
18. Given two non-empty binary trees `s` and `t`, check whether tree `t` has exactly the same structure and node values with a subtree of `s` **[subtree-of-another-tree](subtree-of-another-tree)**
2122

2223
> Questions 8, 16, 17 are part of [Team Blind's curated list](https://github.com/AlgorithmCrackers/Interview-Questions/issues/28)
2324

04_Trees_and_Graphs/isBST/README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
1-
**Write a program to verify whether the given tree is a binary search tree.**
1+
# Validate Binary Search Tree
22

3-
## Recursive solution
3+
![Difficulty-Medium](https://img.shields.io/badge/Difficulty-Medium-yellow)
44

5-
For each node,
6-
- check if it satisfies min/max constraint
7-
- Check recursively for its left and right subtrees
8-
- For left subtree, the node's data is its max
9-
- For right subtree, the node's data is its min
5+
Given a binary tree, determine if it is a valid binary search tree (BST).
106

11-
Time complexity - `O(n)`
7+
Assume a BST is defined as follows:
8+
9+
The left subtree of a node contains only nodes with keys less than the node's key.
10+
The right subtree of a node contains only nodes with keys greater than the node's key.
11+
Both the left and right subtrees must also be binary search trees.
12+
13+
14+
## Example 1:
15+
16+
```
17+
2
18+
/ \
19+
1 3
20+
21+
Input: [2,1,3]
22+
Output: true
23+
```
24+
25+
## Example 2:
26+
27+
```
28+
5
29+
/ \
30+
1 4
31+
/ \
32+
3 6
33+
34+
Input: [5,1,4,null,null,3,6]
35+
Output: false
36+
Explanation: The root node's value is 5 but its right child's value is 4.
37+
```
38+
39+
## Complexity Analysis (recursion based solution):
40+
41+
- **Time complexity** : `O(n)`. We visit each node in the tree exactly once.
42+
43+
- **Space complexity** : `O(log(n)) - best, o(n) worst`. The best case is for a completely balanced tree. The worst case is for an unbalanced tree. The space is to keep a recursion stack.
44+
45+
#### [LeetCode link](https://leetcode.com/problems/validate-binary-search-tree/)

04_Trees_and_Graphs/isBST/solution.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
13+
/* Recursion based solution */
14+
var isValid = (node, min, max) => {
15+
if (!node) return true
16+
// if constraint is not satisified return false
17+
if (!(node.val > min && node.val < max)) return false;
18+
return isValid(node.left, min, node.val) && isValid(node.right, node.val, max)
19+
}
20+
21+
var isValidBST = function(root) {
22+
return isValid(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
23+
};
24+
25+
/*
26+
NOTE: in JS NUMBER.MIN_VALUE is the value closest to 0, not the negative minimum
27+
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Subtree of Another Tree
2+
3+
#### Difficulty in LeetCode
4+
![Difficulty-Easy](https://img.shields.io/badge/Difficulty-Easy-green)
5+
6+
#### Difficulty of understanding complexity
7+
![Difficulty-Medium](https://img.shields.io/badge/Difficulty-Medium-yellow)
8+
9+
Given two non-empty binary trees `s` and `t`, check whether tree t has exactly the same structure and node values with a subtree of `s`. A subtree of `s` is a tree consists of a node in `s` and all of this node's descendants. The tree `s` could also be considered as a subtree of itself.
10+
11+
12+
## Example 1:
13+
14+
```
15+
Given tree s:
16+
17+
3
18+
/ \
19+
4 5
20+
/ \
21+
1 2
22+
Given tree t:
23+
4
24+
/ \
25+
1 2
26+
Return true, because t has the same structure and node values with a subtree of s.
27+
```
28+
29+
## Example 2:
30+
31+
```
32+
Given tree s:
33+
34+
3
35+
/ \
36+
4 5
37+
/ \
38+
1 2
39+
/
40+
0
41+
Given tree t:
42+
4
43+
/ \
44+
1 2
45+
Return false.
46+
```
47+
48+
## Complexity Analysis (Recursive Comparision Approach):
49+
50+
- **Time complexity** : `O(m * n)`. Where `n` is number of nodes in **s** and `m` is number of nodes in **t**.
51+
52+
An alternative approach is to search through the larger tree, Tl , Each time a node in Tl matches the root of T2, call `isSameTree`. The matchTree method will compare the two subtrees to see if they are identical. Analyzing the runtime is somewhat complex. A naive answer would be to say that it is 0(nm) time, where n is the number of nodes in Tl and m is the number of nodes in T2. While this is technically correct, a little more thought can produce a tighter bound
53+
54+
55+
* I think it's because in the worst case, the `isSameTree` method is called for each node. The `isSameTree` method will be O(n) with `n` being the size of the smaller tree. You are calling this `m` times with m being the size of the larger tree, so O(m)O(n) = O(mn)
56+
* If the every element in the bigger tree contains same elements as the smaller tree, each time we need to compare until we find that the smaller tree reaches null but bigger tree's branch not. This process is persistence among all of the tree, so it would be m times n
57+
* For Approach#2 O(mn) is definitely overestimating. The upper bound is closer to O(m + kn) where k is the number of nodes for which the root of t matches the value of the node of s that's just being traversed. To prove this point let's rewrite the traverse method slightly differently and add s.val == t.val && before the equals(s, t) call. Now it's clearer that equals is doing work only when the root of t matches the current node of s. How much work will it do? If the 2 trees match, it will do O(n) work. Otherwise, on any mismatch we'll break early so even O(m + k*n) is an over estimate.
58+
* It's more complicated than that. If all n nodes match the root of the t, then traverse will no longer do O(n) work, unless t itself is also equal to all the same values. But then, it will only do that O(n) work once because of the ||. CTCI solution discusses this problem a little more in depth and looks at the runtime in a probabilistic fashion and proves this point.
59+
- **Space complexity** : `O(log(n)) - best, o(n) worst`. The best case is for a completely balanced tree. The worst case is for an unbalanced tree. The space is to keep a recursion stack. `n` is number of nodes in **s**.
60+
61+
#### [LeetCode link](https://leetcode.com/problems/subtree-of-another-tree/)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} s
10+
* @param {TreeNode} t
11+
* @return {boolean}
12+
*/
13+
var isSameTree = (r, t) => {
14+
if (!r || !t) return r === t
15+
return r.val === t.val && isSameTree(r.left, t.left) && isSameTree(r.right, t.right)
16+
}
17+
18+
var isSubtree = function(s, t) {
19+
return s !== null && (isSameTree(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t))
20+
};

0 commit comments

Comments
 (0)