-
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
ca56be4
commit e5b76dc
Showing
3 changed files
with
120 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,42 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "stack" | ||
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 BSTIterator { | ||
stack<TreeNode*> st; | ||
void partInorder(TreeNode* root) { | ||
if (!root) return; | ||
while (root) { | ||
st.push(root); | ||
root = root->left; | ||
} | ||
} | ||
|
||
public: | ||
BSTIterator(TreeNode* root) { | ||
partInorder(root); | ||
} | ||
|
||
int next() { | ||
TreeNode* top = st.top(); | ||
st.pop(); | ||
if (top->right) partInorder(top->right); | ||
return top->val; | ||
} | ||
|
||
bool hasNext() { | ||
return !st.empty(); | ||
} | ||
}; |
48 changes: 48 additions & 0 deletions
48
leetcode/problems/cpp/binary-tree-zigzag-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,48 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
#include "queue" | ||
#include "algorithm" | ||
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>> zigzagLevelOrder(TreeNode* root) { | ||
vector<vector<int>> ans; | ||
queue<TreeNode*> q; | ||
bool dir = true; | ||
|
||
if (!root) return ans; | ||
q.push(root); | ||
vector<int> tmp; | ||
|
||
while (!q.empty()) { | ||
int size = q.size(); | ||
while (size--) { | ||
TreeNode* front = q.front(); | ||
tmp.push_back(front->val); | ||
q.pop(); | ||
if (front->left) q.push(front->left); | ||
if (front->right) q.push(front->right); | ||
} | ||
if (dir = !dir) | ||
reverse(tmp.begin(), tmp.end()); | ||
ans.push_back(tmp); | ||
tmp.clear(); | ||
} | ||
|
||
return 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,30 @@ | ||
// by @codeAbinash | ||
// Time : O(log n * log 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: | ||
int countNodes(TreeNode* root) { | ||
if (!root) return 0; | ||
TreeNode* left = root, * right = root; | ||
int height = 0; | ||
|
||
while (right) { | ||
left = left->left; | ||
right = right->right; | ||
height++; | ||
} | ||
|
||
if (!left) return (1 << height) - 1; | ||
return 1 + countNodes(root->left) + countNodes(root->right); | ||
} | ||
}; |