Binary Tree Types

1. Full (Proper) Binary Tree # Each nodes has either 2 or 0 children. 12 8 18 5 11 2. Degenerate (Pathological) Binary Tree # Each internal node has either a right or a left child. # Each node is a left node or each node is a right node. 1 1 2 2 3 3 4. Complete Binary Tree # Each level is filled from left to right. 8 5 3 4 6 7 5....

December 10, 2022 · 2 min · 218 words · Me

Leetcode 104 Maximum Depth of Binary Tree

Problem Statement Given the root of a binary tree, return its maximum depth. A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: 3 9 20 15 7 Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Key Insights We can Recursive Solution # Definition for a binary tree node....

December 10, 2022 · 2 min · 321 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