Leetcode - Linked lists
Traversal
94. Binary Tree Inorder Traversal[M]
https://leetcode.com/problems/binary-tree-inorder-traversal/
Description
Given a binary tree, return the inorder traversal of its nodes’ values.
Example:
1 | Input: [1,null,2,3] |
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
https://leetcode.com/problems/binary-tree-inorder-traversal/solution/
1 | # Definition for a binary tree node. |
145. Binary Tree Postorder Traversals[H]
https://leetcode.com/problems/binary-tree-postorder-traversal/
Description
Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
1 | Input: [1,null,2,3] |
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
1 | # Definition for a binary tree node. |
144. Binary Tree Preorder Traversal[M]
https://leetcode.com/problems/binary-tree-preorder-traversal/
Description
Given a binary tree, return the preorder traversal of its nodes’ values.
Example:
1 | Input: [1,null,2,3] |
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution
1 | # Definition for a binary tree node. |
102. Binary Tree Level Order Traversal[M]
https://leetcode.com/problems/binary-tree-level-order-traversal/
Description
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
1 | 3 |
return its level order traversal as:
1 | [ |
Solution
1 | # Definition for a binary tree node. |
103. Binary Tree Zigzag Level Order Traversal[M]
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
Description
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
1 | 3 |
return its zigzag level order traversal as:
1 | [ |
Solution
1 |
105. Construct Binary Tree from Preorder and Inorder Traversal[M]
https://leetcode.com/problems/divide-two-integers/
Description
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
1 | preorder = [3,9,20,15,7] |
Return the following binary tree:
1 | 3 |
Solution
106. Construct Binary Tree from Inorder and Postorder Traversal[M]
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
Description
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
1 | inorder = [9,3,15,20,7] |
Return the following binary tree:
1 | 3 |
Solution
107. Binary Tree Level Order Traversal II[E]
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
Description
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
1 | 3 |
return its bottom-up level order traversal as:
1 | [ |
Solution
1 | # Definition for a binary tree node. |
Unique
95. Unique Binary Search Trees II[M]
https://leetcode.com/problems/unique-binary-search-trees-ii/
Description
iven an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
Example:
1 | Input: 3 |
Constraints:
0 <= n <= 8
Solution
96. Unique Binary Search Trees[M]
https://leetcode.com/problems/unique-binary-search-trees/
Description
Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?
Example:
1 | Input: 3 |
Constraints:
1 <= n <= 19
Solution
https://leetcode.com/discuss/86650/fantastic-clean-java-dp-solution-with-detail-explaination
1 | class Solution: |
99. Recover Binary Search Tree[H]
https://leetcode.com/problems/recover-binary-search-tree/
Description
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Example 1:
1 | Input: [1,3,null,null,2] |
Example 2:
1 | Input: [3,1,4,null,null,2] |
Follow up:
- A solution using O(n) space is pretty straight forward.
- Could you devise a constant space solution?
Solution
1 |
98. Validate Binary Search Tree[M]
https://leetcode.com/problems/validate-binary-search-tree/
Description
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a 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 |
Example 2:
1 | 5 |
Solution
100. Same Tree[E]
https://leetcode.com/problems/same-tree/
Description
Given two binary trees, 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 | Input: 1 1 |
Example 2:
1 | Input: 1 1 |
Example 3:
1 | Input: 1 1 |
Solution
https://leetcode.com/problems/same-tree/solution/
1 | # Definition for a binary tree node. |
1 | /** |
Depth
104. Maximum Depth of Binary Tree[E]
https://leetcode.com/problems/maximum-depth-of-binary-tree/
Description
Given a binary 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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
1 | 3 |
return its depth = 3.
Solution
1 | # Definition for a binary tree node. |
111. Minimum Depth of Binary Tree[E]
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Description
ven a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
1 | 3 |
Solution
Convert
108. Convert Sorted Array to Binary Search Tree[E]
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Description
ven an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
1 | Given the sorted array: [-10,-3,0,5,9], |
Solution
109. Convert Sorted List to Binary Search Tree[M]
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
Description
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
1 | Given the sorted linked list: [-10,-3,0,5,9], |
Solution
1 |
114. Flatten Binary Tree to Linked List[M]
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
Description
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1 | 1 |
The flattened tree should look like:
1 | 1 |
Solution
1 |
110. Balanced Binary Tree[E]
https://leetcode.com/problems/balanced-binary-tree/
Description
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]:
1 | 3 |
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]:
1 | 1 |
Return false.
Solution
124. Binary Tree Maximum Path Sum[H]
https://leetcode.com/problems/binary-tree-maximum-path-sum/
Description
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [-10,9,20,null,null,15,7] |
Solution
226. Invert Binary Tree[E]
https://leetcode.com/problems/invert-binary-tree/
Description
Invert a binary tree.
Example:
Input:
1 | 4 |
Output:
1 | 4 |
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
Solution
1 |
235. Lowest Common Ancestor of a Binary Search Tree[E]
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
Description
Given a binary search tree (BST), find the lowest common ancestor (LCA) 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).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]

Example 1:
1 | Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 |
Example 2:
1 | Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 |
Constraints:
- All of the nodes’ values will be unique.
- p and q are different and both values will exist in the BST.
Solution
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/solution/
236. Lowest Common Ancestor of a Binary Tree[M]
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
Description
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).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:
1 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
Example 2:
1 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
Note:
- All of the nodes’ values will be unique.
- p and q are different and both values will exist in the binary tree.
Solution
257. Binary Tree Paths[E]
https://leetcode.com/problems/binary-tree-paths/
Description
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
1 | Input: |
Solution
543. Diameter of Binary Tree[E]
https://leetcode.com/problems/diameter-of-binary-tree/
Description
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1 | 1 |
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
Solution
https://leetcode.com/problems/diameter-of-binary-tree/solution/
1 | # Definition for a binary tree node. |
617. Merge Two Binary Trees[E]
https://leetcode.com/problems/merge-two-binary-trees/
Description
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
Example 1:
1 | Input: |
Note: The merging process must start from the root nodes of both trees.
Solution
https://leetcode.com/problems/merge-two-binary-trees/solution/
1 | # Definition for a binary tree node. |
654. Maximum Binary Tree[M]
https://leetcode.com/problems/maximum-binary-tree/
Description
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
- The root is the maximum number in the array.
- The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
- The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
1 | Input: [3,2,1,6,0,5] |
Note:
- The size of the given array will be in the range [1,1000].
Solution
https://leetcode.com/problems/maximum-binary-tree/solution/
671. Second Minimum Node In a Binary Tree[E]
https://leetcode.com/problems/divide-two-integers/
Description
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.
If no such second minimum value exists, output -1 instead.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/solution/
1 | # Definition for a binary tree node. |
700. Search in a Binary Search Tree[E]
https://leetcode.com/problems/search-in-a-binary-search-tree/
Description
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL.
For example,
1 | Given the tree: |
You should return this subtree:
1 | 2 |
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
Solution
1 | # Definition for a binary tree node. |
101. Symmetric Tree[E]
https://leetcode.com/problems/symmetric-tree/
Description
Share
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1 | 1 |
But the following [1,2,2,null,3,null,3] is not:
1 | 1 |
Follow up: Solve it both recursively and iteratively.
Solution
208. Implement Trie (Prefix Tree)[M]
https://leetcode.com/problems/implement-trie-prefix-tree//
Description
Implement a trie with insert, search, and startsWith methods.
Example:
1 | Trie trie = new Trie(); |
Note:
- You may assume that all inputs are consist of lowercase letters
a-z. - All inputs are guaranteed to be non-empty strings.
Solution
538. Convert BST to Greater Tree[E]
https://leetcode.com/problems/convert-bst-to-greater-tree/
Description
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
1 | Input: The root of a Binary Search Tree like this: |
Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
Solution
572. Subtree of Another Tree[E]
https://leetcode.com/problems/subtree-of-another-tree/
Description
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
1 | 3 |
Given tree t:
1 | 4 |
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
1 | 3 |
Given tree t:
1 | 4 |
Return false.
Solution
872. Leaf-Similar Trees[E]
https://leetcode.com/problems/leaf-similar-trees/
Description
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
Constraints:
- Both of the given trees will have between
1and200nodes. - Both of the given trees will have values between
0and200
Solution
116. Populating Next Right Pointers in Each Node[M]
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
Description
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1 | struct Node { |
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.
Follow up:
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
Example 1:

1 | Input: root = [1,2,3,4,5,6,7] |
Constraints:
- The number of nodes in the given tree is less than
4096. -1000 <= node.val <= 1000
Solution
1 |
117. Populating Next Right Pointers in Each Node II[M]
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
Description
Given a binary tree
1 | struct Node { |
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.
Follow up:
- You may only use constant extra space.
- Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
Example 1:

1 | Input: root = [1,2,3,4,5,null,7] |
Constraints:
- The number of nodes in the given tree is less than
6000. -100 <= node.val <= 100
Solution
1 |





