1.
Binary Tree
2.
Maximum Depth of Binary Tree (Easy)
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.
3.
Same Tree (Easy)
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.
4.
Invert Binary Tree (Easy)
Given the root
of a binary tree, invert the tree, and return its root.
5.
Symmetric Tree (Easy)
Given the root
of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
6.
Path Sum (Easy)
Given the root
of a binary tree and an integer targetSum
, return true
if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum
.
A leaf is a node with no children.
7.
Count Complete Tree Nodes (Easy)
Given the root
of a complete binary tree, return the number of the nodes in the tree.
In a complete binary tree, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h
.
Design an algorithm that runs in less than O(n)
time complexity.
8.
Average of Levels in Binary Tree (Easy)
Given the root
of a binary tree, return the average value of the nodes on each level in the form of an array.
9.
Minimum Absolute Difference in BST (Easy)
Given the root
of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
10.
Construct Binary Tree from Preorder and Inorder Traversal (Medium)
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.
preorder
and inorder
consist of unique values.
11.
Construct Binary Tree from Inorder and Postorder Traversal (Medium)
Given two integer arrays inorder
and postorder
where inorder
is the inorder traversal of a binary tree and postorder
is the postorder traversal of the same tree, construct and return the binary tree.
12.
Populating Next Right Pointers in Each Node II (Medium)
Given a binary tree with nodes:
class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Write a solution that uses constant space. Recursion using implicit stack space is fine.
13.
Flatten Binary Tree to Linked List (Medium)
Given the root
of a binary tree, flatten the tree into a "linked list":
- The "linked list" should use the same
TreeNode
class where theright
child pointer points to the next node in the list and theleft
child pointer is alwaysnull
. - The "linked list" should be in the same order as a pre-order traversal of the binary tree.
14.
Sum Root to Leaf Numbers (Medium)
You are given the root
of a binary tree containing digits from 0
to 9
only.
Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3
represents the number 123
.
Return the total sum of all root-to-leaf numbers. A leaf node is a node with no children.
15.
Binary Search Tree Iterator (Medium)
Implement the BSTIterator
class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root)
Initializes an object of theBSTIterator
class. Theroot
of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.boolean hasNext()
Returnstrue
if there exists a number in the traversal to the right of the pointer, otherwise returnsfalse
.int next()
Moves the pointer to the right, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next()
will return the smallest element in the BST.
You may assume that next()
calls will always be valid. That is, there will be at least a next number in the in-order traversal when next()
is called.
Implement next()
and hasNext()
to run in average O(1)
time and use O(h)
memory, where h
is the height of the tree.
16.
Lowest Common Ancestor of a Binary Tree (Medium)
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
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).
17.
Binary Tree Right Side View (Medium)
Given the root
of a binary tree, imagine yourself standing on the right side of it, and return the values of the nodes you can see ordered from top to bottom.
18.
Binary Tree Level Order Traversal (Medium)
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).
19.
Binary Tree Zigzag Level Order Traversal (Medium)
Given the root
of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
20.
Kth Smallest Element in a BST (Medium)
Given the root
of a binary search tree, and an integer k
, return the k
th smallest value (1-indexed) of all the values of the nodes in the tree.
21.
Validate Binary Search Tree (Medium)
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.