-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bb8a75
commit 064d533
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
50 changes: 50 additions & 0 deletions
50
leetcode/problems/cpp/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |