This file contains hidden or 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
//Runtime: 8 ms, faster than 66.59% | |
class Solution { | |
public: | |
bool hasCycle(ListNode *head) { | |
ListNode *fast = head; | |
ListNode *slow = head; | |
while(fast && fast->next){ | |
slow = slow->next; |
This file contains hidden or 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
//Runtime: 40 ms, faster than 36.67% | |
class Solution { | |
public: | |
int curSum = 0; | |
void travelBST(TreeNode* root){ | |
if(!root) return; | |
if(root->right) travelBST(root->right); | |
This file contains hidden or 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
//Runtime: 4 ms, faster than 91.80% | |
class Solution { | |
public: | |
int temp; | |
bool flag = true; | |
bool isUnivalTree(TreeNode* root) { | |
if(!root) return NULL; | |
temp = root->val; |
This file contains hidden or 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
//Runtime: 0 ms, faster than 100.00% | |
#include <ios> | |
static auto fastInput = []() { | |
ios_base::sync_with_stdio(false),cin.tie(nullptr); | |
return 0; | |
}(); | |
class Solution { | |
public: |
This file contains hidden or 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
//Runtime: 0 ms, faster than 100.00% | |
class Solution { | |
public: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> nodes; | |
stack<TreeNode*> todo; | |
TreeNode* cur = root; | |
while(cur || !todo.empty()){ | |
if(cur){ |
This file contains hidden or 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
//Runtime: 0 ms, faster than 100.00% | |
#include <ios> | |
static auto fastInput = []() { | |
ios_base::sync_with_stdio(false),cin.tie(nullptr); | |
return 0; | |
}(); | |
class Solution { | |
public: |
This file contains hidden or 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
//Runtime: 8 ms, faster than 39.55% | |
class Solution { | |
public: | |
bool isSymmetric(TreeNode* root) { | |
TreeNode* t1, *t2; | |
queue<TreeNode*> q; | |
q.push(root); | |
q.push(root); | |
while(!q.empty()){ |
This file contains hidden or 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
//Runtime: 28 ms, faster than 30.42% | |
class Solution { | |
vector<TreeNode*> nodes; | |
public: | |
bool isSubtree(TreeNode* s, TreeNode* t) { | |
if(!s && !t) return true; | |
if(!s || !t) return false; | |
getDepth(s,getDepth(t,-1)); |
This file contains hidden or 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
//20ms 50% | |
class Solution { | |
public: | |
bool isPalindrome(ListNode* head) { | |
if(!head || !head->next) | |
return true; | |
vector<int> array; | |
array.push_back(head->val); | |
while(head->next != NULL){ |
This file contains hidden or 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
// 36 ms | |
TreeNode* searchBST(TreeNode* root, int val) { | |
while (root != nullptr && root->val != val) { | |
root = (root->val > val) ? root->left : root->right; | |
} | |
return root; | |
} |