Lunski's Clutter

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

0%

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Example 1:

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

Example 2:

1
2
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Read more »

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 »