Skip to main content
559, Maximum Depth of N-ary Tree

I Problem

Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).


MikeAbout 3 minbinary treeeasytreedepth first searchbreadth first search
572, Subtree of Another Tree

I Problem

Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.

A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.


MikeAbout 3 minbinary treeeasybinary treedepth first searchstring matchinghash function
100, Same Tree

I Problem

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

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


MikeAbout 2 minbinary treeeasybinary treedepth first searchbreadth first search
109, Convert Sorted List to Binary Search Tree

I Problem

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.

Example 1

Input: head = [-10, -3, 0, 5, 9]
Output: [0, -3, 9, -10, null, 5]
Explanation: One possible answer is [0, -3, 9, -10, null, 5], which represents the shown height balanced BST.


MikeAbout 2 minbinary treemediumlinked listbinary treebinary search treedivide and conquer
108, Convert Sorted Array to Binary Search Tree

I Problem

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

Example 1

Input: nums = [-10, -3, 0, 5, 9]
Output: [0, -3, 9, -10, null, 5]
Explanation: [0, -10, 5, null, -3, null, 9] is also accepted:


MikeAbout 3 minbinary treeeasytreebinary treebinary search treearraydivide and conquer
669, Trim a Binary Search Tree

I Problem

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.


MikeAbout 2 minbinary treemediumbinary treebinary search treedepth first search
450, Delete Node in a BST

I Problem

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Basically, the deletion can be divided into two stages:

  1. Search for a node to remove.
  2. If the node is found, delete the node.

MikeAbout 3 minbinary treemediumbinary treebinary search tree
701, Insert into a Binary Search Tree

I Problem

You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.


MikeAbout 3 minbinary treemediumbinary treebinary search tree
235, Lowest Common Ancestor of a Binary Search Tree

I Problem

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself)."


MikeAbout 4 minbinary treemediumtreebinary treebinary search treedepth first search
236, Lowest Common Ancestor of a Binary Tree

I Problem

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself)."


MikeAbout 2 minbinary treemediumbinary treedepth first search
2
3
4
5