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

Leetcode 733: Flood Fill

Problem Statement An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected >4-directionally to the starting pixel of the same color as the starting pixel, >plus any pixels connected 4-directionally to those pixels (also with the same >color), and so on....

December 3, 2022 · 3 min · 443 words · Me