Leetcode 236 Lowest Common Ancestor of a Binary Tree

Introduction Daniel (🧔🏽‍♂️) nodded his head slowly as he added cream to his freshly poured coffee. He had worked hard to learn his Leetcode and he was feeling ready. He took a sip and walked over to his laptop and sat down. Moments later the interview began. 👩 “Good afternoon Daniel! Thanks for taking the time to interview with me today. Let’s begin with some brief introductions. My name is Jordanna!...

January 7, 2023 · 9 min · 1821 words · Me

Leetcode 208 Implement Trie

Problem Statement A trie (pronounced as “try”) or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Returns true if the string word is in the trie (i....

December 19, 2022 · 3 min · 467 words · Me

Leetcode 102 Binary Tree Level Order Traversal

Problem Statement Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level). Example 1: 3 9 20 16 7 Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Example 3: Input: root = [] Output: [] Solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self....

December 9, 2022 · 1 min · 186 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