Skip to content

Commit

Permalink
formatted some cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Dec 14, 2023
1 parent ce0b6f2 commit ce14ae1
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 140 deletions.
81 changes: 41 additions & 40 deletions Data-Structures/Trees/problems/check-for-balanced-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,57 @@
using namespace std;

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) {}
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:
// a function to check if tree is balanced or not on basis of it's height.
bool isBalanced(TreeNode *root) { return dfsHeight(root) != -1; }
public:
// a function to check if tree is balanced or not on basis of it's height.
bool isBalanced(TreeNode* root) { return dfsHeight(root) != -1; }

/* a function to calculate height of tree, if this function returns -1,
the tree is not balanced. The tree will always be balanced if this function
returns some non-negative height */
/* a function to calculate height of tree, if this function returns -1,
the tree is not balanced. The tree will always be balanced if this
function returns some non-negative height */

int dfsHeight(TreeNode *root) {
if (root == NULL)
return 0;
int dfsHeight(TreeNode* root) {
if (root == NULL)
return 0;

int lh = dfsHeight(root->left);
if (lh == -1)
return -1;
int lh = dfsHeight(root->left);
if (lh == -1)
return -1;

int rh = dfsHeight(root->right);
if (rh == -1)
return -1;
int rh = dfsHeight(root->right);
if (rh == -1)
return -1;

// for a balanced BT, height(left sub-tree) - height(right sub-tree) <= 1
if (abs(lh - rh) > 1)
return -1;
// for a balanced BT, height(left sub-tree) - height(right sub-tree) <=
// 1
if (abs(lh - rh) > 1)
return -1;

return max(lh, rh) + 1;
}
return max(lh, rh) + 1;
}
};

int main() {
Solution solution;

TreeNode *balancedTree = new TreeNode(1);
balancedTree->left = new TreeNode(2);
balancedTree->right = new TreeNode(3);
balancedTree->left->left = new TreeNode(4);
balancedTree->left->right = new TreeNode(5);
balancedTree->right->left = new TreeNode(6);
balancedTree->right->right = new TreeNode(7);

bool isBalancedResult = solution.isBalanced(balancedTree);
cout << "tree balanced? " << (isBalancedResult ? "Yes" : "No") << endl;
return 0;
Solution solution;

TreeNode* balancedTree = new TreeNode(1);
balancedTree->left = new TreeNode(2);
balancedTree->right = new TreeNode(3);
balancedTree->left->left = new TreeNode(4);
balancedTree->left->right = new TreeNode(5);
balancedTree->right->left = new TreeNode(6);
balancedTree->right->right = new TreeNode(7);

bool isBalancedResult = solution.isBalanced(balancedTree);
cout << "tree balanced? " << (isBalancedResult ? "Yes" : "No") << endl;
return 0;
}
76 changes: 38 additions & 38 deletions Data-Structures/Trees/problems/depth-of-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
int val;
TreeNode* left;
TreeNode* right;
explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == NULL)
return 0;
int lh = maxDepth(root->left);
int rh = maxDepth(root->right);
return 1 + max(lh, rh); // formula
}
int minDepth(TreeNode *root) {
if (root == NULL)
return 0;

// if the tree is a skew tree
if (root->left == NULL && root->right != NULL) {
return minDepth(root->right) + 1;
} else if (root->left != NULL && root->right == NULL) {
return minDepth(root->left) + 1;
public:
int maxDepth(TreeNode* root) {
if (root == NULL)
return 0;
int lh = maxDepth(root->left);
int rh = maxDepth(root->right);
return 1 + max(lh, rh); // formula
}
int minDepth(TreeNode* root) {
if (root == NULL)
return 0;

// if the tree is a skew tree
if (root->left == NULL && root->right != NULL) {
return minDepth(root->right) + 1;
} else if (root->left != NULL && root->right == NULL) {
return minDepth(root->left) + 1;
}

// if a normal binary tree
int lh = minDepth(root->left);
int rh = minDepth(root->right);
return 1 + min(lh, rh); // formula
}

// if a normal binary tree
int lh = minDepth(root->left);
int rh = minDepth(root->right);
return 1 + min(lh, rh); // formula
}
};

int main() {
TreeNode *root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);

Solution solution;
Solution solution;

int maxDepth = solution.maxDepth(root);
int minDepth = solution.maxDepth(root);
std::cout << "Max Depth of tree : " << maxDepth << std::endl;
std::cout << "Min Depth of tree : " << minDepth << std::endl;
int maxDepth = solution.maxDepth(root);
int minDepth = solution.maxDepth(root);
std::cout << "Max Depth of tree : " << maxDepth << std::endl;
std::cout << "Min Depth of tree : " << minDepth << std::endl;

return 0;
return 0;
}
62 changes: 31 additions & 31 deletions Data-Structures/Trees/problems/diameter-of-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

/*
Expand All @@ -16,39 +16,39 @@ between them.
*/

class Solution {
public:
int diameter = 0;
int diameterOfBinaryTree(TreeNode *root) {
height(root);
return diameter;
}
int height(TreeNode *node) {
if (!node)
return 0;
int lh = height(node->left);
int rh = height(node->right);
diameter = max(diameter, lh + rh);
return 1 + max(lh, rh);
}
public:
int diameter = 0;
int diameterOfBinaryTree(TreeNode* root) {
height(root);
return diameter;
}
int height(TreeNode* node) {
if (!node)
return 0;
int lh = height(node->left);
int rh = height(node->right);
diameter = max(diameter, lh + rh);
return 1 + max(lh, rh);
}
};

int main() {
TreeNode *root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(9);
root->right = new TreeNode(20);
root->right->left = new TreeNode(15);
root->right->right = new TreeNode(7);

Solution solution;
Solution solution;

int result = solution.diameterOfBinaryTree(root);
int result = solution.diameterOfBinaryTree(root);

cout << "Diameter of the binary tree: " << result << endl;
cout << "Diameter of the binary tree: " << result << endl;

delete root->left;
delete root->right->left;
delete root->right->right;
delete root;
delete root->left;
delete root->right->left;
delete root->right->right;
delete root;

return 0;
return 0;
}
63 changes: 32 additions & 31 deletions Data-Structures/Trees/problems/symmetric-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

/*
Expand All @@ -14,36 +14,37 @@ the root node are mirror images of each other.
*/

class Solution {
public:
bool isSymmetric(TreeNode *root) {
return root == NULL || helper(root->left, root->right);
}
bool helper(TreeNode *left, TreeNode *right) {
if (left == NULL || right == NULL)
return left == right;
if (left->val != right->val)
return false;
return helper(left->left, right->right) && helper(left->right, right->left);
}
public:
bool isSymmetric(TreeNode* root) {
return root == NULL || helper(root->left, root->right);
}
bool helper(TreeNode* left, TreeNode* right) {
if (left == NULL || right == NULL)
return left == right;
if (left->val != right->val)
return false;
return helper(left->left, right->right) &&
helper(left->right, right->left);
}
};

int main() {
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(3);
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(2);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(3);

Solution solution;
bool isSymmetric = solution.isSymmetric(root);
Solution solution;
bool isSymmetric = solution.isSymmetric(root);

if (isSymmetric) {
cout << "The tree is symmetric." << endl;
} else {
cout << "The tree is not symmetric." << endl;
}
if (isSymmetric) {
cout << "The tree is symmetric." << endl;
} else {
cout << "The tree is not symmetric." << endl;
}

return 0;
}
return 0;
}

0 comments on commit ce14ae1

Please sign in to comment.