-
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
13a74e1
commit 14b4270
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
leetcode/problems/cpp/binary-tree-level-order-traversal.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,40 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(1) | ||
|
||
#include "vector" | ||
#include "queue" | ||
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) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> levelOrder(TreeNode* root) { | ||
vector<vector<int>> res; | ||
if (!root) return res; | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
while (!q.empty()) { | ||
int size = q.size(); | ||
vector<int> level; | ||
while (size--) { | ||
TreeNode* node = q.front(); | ||
q.pop(); | ||
level.push_back(node->val); | ||
if (node->left) q.push(node->left); | ||
if (node->right) q.push(node->right); | ||
} | ||
res.push_back(level); | ||
} | ||
return res; | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
leetcode/problems/cpp/minimum-distance-between-bst-nodes.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,33 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(1) | ||
|
||
#include "climits" | ||
int min(int a, int b) { return a > b ? b : a; } | ||
|
||
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 { | ||
int minDiff = INT_MAX; | ||
int prev = -1; | ||
void inorder(TreeNode* root) { | ||
if (!root) return; | ||
inorder(root->left); | ||
if (prev != -1) minDiff = min(minDiff, root->val - prev); | ||
prev = root->val; | ||
inorder(root->right); | ||
} | ||
public: | ||
int minDiffInBST(TreeNode* root) { | ||
inorder(root); | ||
return minDiff; | ||
} | ||
}; |
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,23 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(1) | ||
|
||
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 isSameTree(TreeNode* p, TreeNode* q) { | ||
if (!q && !p) return true; | ||
if (!q || !p) return false; | ||
if (p->val == q->val) | ||
return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right)); | ||
return false; | ||
} | ||
}; |
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,26 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(1) | ||
|
||
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 isSameTreeSymmetric(TreeNode* p, TreeNode* q) { | ||
if (!q && !p) return true; | ||
if (!q || !p) return false; | ||
if (p->val == q->val) | ||
return (isSameTreeSymmetric(p->left, q->right) && isSameTreeSymmetric(p->right, q->left)); | ||
return false; | ||
} | ||
bool isSymmetric(TreeNode* root) { | ||
return isSameTreeSymmetric(root->left, root->right); | ||
} | ||
}; |