Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:

1
2
3
4
5
6
7
8
9
Input: [1,2,3,4,5]

1
/ \
2 3
/ \
4 5

Output: [[4,5,3],[2],[1]]

Explanation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1. Removing the leaves [4,5,3] would result in this tree:

1
/
2


2. Now removing the leaf [2] would result in this tree:

1


3. Now removing the leaf [1] would result in the empty tree:

[]
Read more »

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:

1
2
Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

1
2
Input: root = [1,null,2]
Output: 2

Example 3:

1
2
Input: root = []
Output: 0

Example 4:

1
2
Input: root = [0]
Output: 1
Read more »

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:

1
2
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:

1
2
Input: root = [1]
Output: [[1]]

Example 3:

1
2
Input: root = []
Output: []
Read more »

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:

1
2
Input: p = [1,2,3], q = [1,2,3]
Output: true

Example 2:

1
2
Input: p = [1,2], q = [1,null,2]
Output: false

Example 3:

1
2
Input: p = [1,2,1], q = [1,1,2]
Output: false
Read more »

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.

Example 1:

1
2
Input: root = [2,1,3]
Output: true

Example 2:

1
2
3
Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4
Read more »