Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dfs #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Dfs #75

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions DFS/ValidateBinarySearchTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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:
bool isValidBST(TreeNode* root)
{
stack<TreeNode*> s;
TreeNode* pre = NULL;

while(root || !s.empty())
{
while(root)
{
s.push(root);
root = root-> left;

}
root = s.top(), s.pop();

if(pre != NULL && root-> val <= pre-> val)
return false;
pre = root;
root=root->right;

}
return true;
}
};
7 changes: 6 additions & 1 deletion DP/00-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
*List of Questions Added*

1. fibonacci seq
1. ways to nth step
2. ways to nth step
3. edit distance
4. Increasing Sequence
5. Best Time to Buy and Sell Stock II


File renamed without changes.
2 changes: 2 additions & 0 deletions DP/IncreasingSequence.cpp → DP/4-IncreasingSequence.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <bits/stdc++.h>
#include<math.h>
#include<iostream>
using namespace std;

/* lis() returns the length of the longest
Expand Down
10 changes: 10 additions & 0 deletions DP/5-BestTimeToBuyandSellStockII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
int maxProfit(vector<int>& P) {
int profit = 0;
for(int i = 1; i < size(P); i++)
if(P[i] > P[i-1]) // yesterday was valley, today is peak
profit += P[i] - P[i-1]; // buy yesterday, sell today
return profit;
}
};