Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2761 from Abe0770/main
Browse files Browse the repository at this point in the history
  • Loading branch information
felivalencia3 authored Aug 5, 2023
2 parents 20ae103 + 824485c commit 4a14fa1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cpp/0144-binary-tree-preorder-traversal.cpp
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;
}
};
45 changes: 45 additions & 0 deletions cpp/0701-insert-into-a-binary-search-tree.cpp
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;
}
};
30 changes: 30 additions & 0 deletions cpp/2348-number-of-zero-filled-subarrays.cpp
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;
}
};

0 comments on commit 4a14fa1

Please sign in to comment.