Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Sep 3, 2023
1 parent 1bb8a75 commit 064d533
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
60 changes: 60 additions & 0 deletions leetcode/problems/cpp/binary-tree-right-side-view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

#include "queue"
#include "vector"
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) {}
};


// Using BFS
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ans;
if (!root) return ans;

queue<TreeNode*> q;
q.push(root);

while (!q.empty()) {
int len = q.size();
ans.push_back(q.back()->val);
while (len--) {
TreeNode* front = q.front();
q.pop();
if (front->left) q.push(front->left);
if (front->right) q.push(front->right);
}
}
return ans;
}
};


// Using DFS
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ans;
rightView(root, 0, ans);
return ans;
}
void rightView(TreeNode* root, int level, vector<int>& ans) {
if (!root) return;
if (level == ans.size())
ans.push_back(root->val);
rightView(root->right, level + 1, ans);
rightView(root->left, level + 1, ans);
}
};
28 changes: 28 additions & 0 deletions leetcode/problems/cpp/evaluate-boolean-binary-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

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) {}
};


// Little bit optimized
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if (!root) return true;
bool leftResult = evaluateTree(root->left);
if (leftResult == true) if (root->val == 2) return true; // Need not to traverse the right part
if (leftResult == false) if (root->val == 3) return false; // Need not to traverse the right part
if (root->val == 2) return leftResult || evaluateTree(root->right);
if (root->val == 3) return leftResult && evaluateTree(root->right);
if (root->val == 0) return false;
return true;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)


#include "locale"
#include "queue"
using namespace std;

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


// Using DFS
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
if (!original) return nullptr;
if (original->val == target->val) return cloned;

TreeNode* left = getTargetCopy(original->left, cloned->left, target);
TreeNode* right = getTargetCopy(original->right, cloned->right, target);

return left ? left : right;
}
};


// Using BFS
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
queue<TreeNode*> q;
q.push(cloned);
TreeNode* curr;
while (true) {
curr = q.front(); q.pop();
if (curr) {
if (curr->val == target->val) break;
q.push(curr->left);
q.push(curr->right);
}
}
return curr;
}
};

0 comments on commit 064d533

Please sign in to comment.