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 110 Balanced Binary Tree

Problem Statement Given a binary tree, determine if it is height-balanced A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Example 1: 3 9 20 15 7 Input: root = [3,9,20,null,null,15,7] Output: true Example 2: 1 2 2 3 3 4 4 Input: root = [1,2,2,3,3,null,null,4,4] Output: false Example 3: Input: root = [] Output: true Key Insights A binary tree is considered height balanced if the height differences between the left and right subtrees are less than or equal to one....

December 9, 2022 · 3 min · 487 words · Me

Leetcode 226 Invert Binary Tree

Problem Statement Given the root of a binary tree, invert the tree, and return its root. Example 1: 4 4 ----> 2 7 7 2 1 3 6 9 9 6 3 1 Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Example 2: Input: root = [2,1,3] Output: [2,3,1] Example 3: Input: root = [] Output: [] Key Insights If you can swap every node’s left and right children, you can “invert” the binary tree....

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

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
Oranges with cinnamon sticks (before they begin to rot)

Leetcode 994 Rotting Oranges

Problem Statement You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1....

December 6, 2022 · 2 min · 279 words · Me