forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2761 from Abe0770/main
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Given the root of a binary tree, return the preorder traversal of its nodes' values. | ||
Ex. Input: root = [1,null,2,3] | ||
Output: [1,2,3] | ||
Time : O(N) | ||
Space : O(H) -> H = Height of the binary tree | ||
*/ | ||
|
||
/** | ||
* 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: | ||
vector <int> res; | ||
|
||
vector<int> preorderTraversal(TreeNode* root) { | ||
if(root != NULL) { | ||
res.push_back(root -> val); | ||
preorderTraversal(root -> left); | ||
preorderTraversal(root -> right); | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
You are given the root node of a binary search tree (BST) and a value to insert into the tree. | ||
Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. | ||
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. | ||
You can return any of them. | ||
Ex. Input: root = [4,2,7,1,3], val = 5 | ||
Output: [4,2,7,1,3,5] | ||
Time : O(N) | ||
Space : O(N) | ||
*/ | ||
|
||
/** | ||
* 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: | ||
TreeNode * create(int val) { | ||
TreeNode *n = new TreeNode; | ||
n -> val = val; | ||
n -> left = n -> right = NULL; | ||
return n; | ||
} | ||
|
||
TreeNode* insertIntoBST(TreeNode* root, int val) { | ||
if(root == NULL) | ||
return create(val); | ||
|
||
else if(val > root -> val) | ||
root -> right = insertIntoBST(root -> right, val); | ||
else if(val < root -> val) | ||
root -> left = insertIntoBST(root -> left, val); | ||
return root; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Given an integer array nums, return the number of subarrays filled with 0. | ||
A subarray is a contiguous non-empty sequence of elements within an array. | ||
Ex. Input: nums = [1,3,0,0,2,0,0,4] | ||
Output: 6 | ||
Explanation: | ||
There are 4 occurrences of [0] as a subarray. | ||
There are 2 occurrences of [0,0] as a subarray. | ||
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6. | ||
Time : O(N) | ||
Space : O(1) | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
long long zeroFilledSubarray(vector<int>& nums) { | ||
long long res = 0, count = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
if (nums[i]) { | ||
res += (count * (count + 1)) / 2; | ||
count = 0; | ||
} else | ||
++count; | ||
} | ||
res += (count * (count + 1)) / 2; | ||
return res; | ||
} | ||
}; |