Leetcode 100 Same Tree

Problem Statement Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1: 2 2 1 3 1 3 Input: p = [1,2,3], q = [1,2,3] Output: true Example 2: 1 1 2 2 Input: p = [1,2], q = [1,null,2] Output: false Example 3: 2 1 1 1 1 2 Input: p = [1,2,1], q = [1,1,2] Output: false Recursive Implementation # Definition for a binary tree node....

December 8, 2022 · 2 min · 343 words · Me

Leetcode 127 Word Ladder

Problem Statement A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList. sk == endWord Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists....

December 8, 2022 · 3 min · 475 words · Me

Leetcode 48 Rotate Image

Problem Statement You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: Input: matrix = \ [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] Output: [ [7, 4, 1], [8, 5, 2], [9, 6, 3] ] Example 2:...

December 7, 2022 · 2 min · 378 words · Me

Leetcode 721 Accounts Merge

Problem Statement Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account. Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name....

December 7, 2022 · 3 min · 559 words · Me

Union Find

Introduction The Union Find data structure stores a collections of disjoint (non-overlapping) sets and can be used to model connected components in undirected graphs. This data structure can be used to: determine if two vetices belong to the same component detect cycles in a graph find the minimum spanning tree of a graph Union Find Implementation Optimized Union Find (Disjoint Set) python implementation with path compression and union by rank....

December 7, 2022 · 2 min · 232 words · Me