Skip to content

Commit 4a14fa1

Browse files
Merge pull request #2761 from Abe0770/main
2 parents 20ae103 + 824485c commit 4a14fa1

3 files changed

+109
-0
lines changed

Diff for: cpp/0144-binary-tree-preorder-traversal.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Given the root of a binary tree, return the preorder traversal of its nodes' values.
3+
4+
Ex. Input: root = [1,null,2,3]
5+
Output: [1,2,3]
6+
7+
Time : O(N)
8+
Space : O(H) -> H = Height of the binary tree
9+
*/
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
vector <int> res;
25+
26+
vector<int> preorderTraversal(TreeNode* root) {
27+
if(root != NULL) {
28+
res.push_back(root -> val);
29+
preorderTraversal(root -> left);
30+
preorderTraversal(root -> right);
31+
}
32+
return res;
33+
}
34+
};

Diff for: cpp/0701-insert-into-a-binary-search-tree.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
You are given the root node of a binary search tree (BST) and a value to insert into the tree.
3+
Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
4+
5+
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion.
6+
You can return any of them.
7+
8+
Ex. Input: root = [4,2,7,1,3], val = 5
9+
Output: [4,2,7,1,3,5]
10+
11+
Time : O(N)
12+
Space : O(N)
13+
*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* struct TreeNode {
18+
* int val;
19+
* TreeNode *left;
20+
* TreeNode *right;
21+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
23+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
24+
* };
25+
*/
26+
class Solution {
27+
public:
28+
TreeNode * create(int val) {
29+
TreeNode *n = new TreeNode;
30+
n -> val = val;
31+
n -> left = n -> right = NULL;
32+
return n;
33+
}
34+
35+
TreeNode* insertIntoBST(TreeNode* root, int val) {
36+
if(root == NULL)
37+
return create(val);
38+
39+
else if(val > root -> val)
40+
root -> right = insertIntoBST(root -> right, val);
41+
else if(val < root -> val)
42+
root -> left = insertIntoBST(root -> left, val);
43+
return root;
44+
}
45+
};

Diff for: cpp/2348-number-of-zero-filled-subarrays.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Given an integer array nums, return the number of subarrays filled with 0.
3+
A subarray is a contiguous non-empty sequence of elements within an array.
4+
5+
Ex. Input: nums = [1,3,0,0,2,0,0,4]
6+
Output: 6
7+
Explanation:
8+
There are 4 occurrences of [0] as a subarray.
9+
There are 2 occurrences of [0,0] as a subarray.
10+
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.
11+
12+
Time : O(N)
13+
Space : O(1)
14+
*/
15+
16+
class Solution {
17+
public:
18+
long long zeroFilledSubarray(vector<int>& nums) {
19+
long long res = 0, count = 0;
20+
for (int i = 0; i < nums.size(); i++) {
21+
if (nums[i]) {
22+
res += (count * (count + 1)) / 2;
23+
count = 0;
24+
} else
25+
++count;
26+
}
27+
res += (count * (count + 1)) / 2;
28+
return res;
29+
}
30+
};

0 commit comments

Comments
 (0)