Skip to content
Open
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
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "shell",
"command": "g++",
"args": [
"./week-4/C++/daijon-bereolacarson.cpp",
"./week-8/C++/daijon-bereolacarson.cpp",
"-o",
"${workspaceFolderBasename}"
],
Expand Down
89 changes: 89 additions & 0 deletions week-6/C++/daijon-bereolacarson.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <deque>
#include <stdlib.h>

using namespace std;

class Node{
public:
int data;
Node *left,*right;
};

//Create a new node
Node* newNode(int data){
Node* node = new Node();
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}

void newTree(Node* &root){
root = newNode(6);
root->left = newNode(3);
root->right = newNode(8);
root->right->right = newNode(4);
root->right->left = newNode(2);
root->right->left->left = newNode(1);
root->right->left->right = newNode(7);

}

void printTree(Node* root){
if(root == NULL)
return;

cout << root->data << " -> ";
printTree(root->left);
printTree(root->right);
}

//1st problem
bool checkLeaf(Node* root){
if(root->left == NULL && root->right == NULL){
return true;
}
else
return false;
}

void findMinimum(Node* root, int &min){
if(root == NULL)
return;
else{
if(root->data < min){
min = root->data;
}
}
findMinimum(root->left, min);
findMinimum(root->right, min);
}

void maxDifference(Node* root, int max, int &finalMax){
int min = 99;
findMinimum(root, min);
max = root->data - min;

if(max > finalMax)
finalMax = max;

if(!checkLeaf(root)){
maxDifference(root->left, -1, finalMax);
maxDifference(root->right, -1, finalMax);
}
return;
}

int main(){
Node* root = NULL;
newTree(root);
cout << "Tree: ";
printTree(root);
cout << "NULL" << endl;

int max_diff = -1;
maxDifference(root, -1, max_diff);
cout << "Max Difference: " << max_diff << endl;
return 0;
}
Empty file removed week-6/C++/main.cpp
Empty file.