Problem:
Remove Duplicates from sorted LL
Leaf-Similar Trees
// Remove Duplicates
void remove(ListNode* &head, ListNode* &temp){
if(temp == head){
head = head -> next;
temp -> next = nullptr;
return;
}
ListNode* curr = head;
ListNode* prev = nullptr;
while(curr != temp){
prev = curr;
curr = curr -> next;
}
prev -> next = curr -> next;
curr -> next = nullptr;
delete curr;
}
ListNode* deleteDuplicates(ListNode* head) {
if(head == nullptr || head -> next == nullptr){
return head;
}
ListNode* curr = head;
while(curr != nullptr ){
if((curr -> next != nullptr) && ((curr -> val) == (curr -> next -> val)) ){
ListNode* track = curr;
curr = curr -> next;
remove (head, track);
}
else{
curr = curr -> next;
}
}
return head;
}
// Leaf-Similar Tree
if(root == NULL) return;
travel(root -> left, v);
if(root -> left == NULL && root -> right == NULL){
v.push_back(root -> val);
}
travel(root -> right,v);
}
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> v1;
vector<int> v2;
travel(root1, v1);
travel(root2,v2);
if(v1.size() != v2.size()) return false;
for(auto i = 0; i<v1.size(); i++){
if(v1[i] != v2[i]) return false;
}
return true;
}