- Basics
- LeetCode
- 144. Binary Tree Preorder Traversal
- 589. N-ary Tree Preorder Traversal
- 145. Binary Tree Postorder Traversal
- 590. N-ary Tree Postorder Traversal
- 94. Binary Tree Inorder Traversal
- 102. Binary Tree Level Order Traversal
- 107. Binary Tree Level Order Traversal II
- 199. Binary Tree Right Side View
- 637. Average of Levels in Binary Tree
- 429. N-ary Tree Level Order Traversal
- 114. Flatten Binary Tree to Linked List
- 226. Invert Binary Tree
- 101. Symmetric Tree
- 104. Maximum Depth of Binary Tree
- 559. Maximum Depth of N-ary Tree
- 111. Minimum Depth of Binary Tree
- 222. Count Complete Tree Nodes
- 110. Balanced Binary Tree
- 257. Binary Tree Paths
- 100. Same Tree
- 404. Sum of Left Leaves
- 513. Find Bottom Left Tree Value
- 112. Path Sum
- 113. Path Sum II
- 437. Path Sum III
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 654. Maximum Binary Tree
- 617. Merge Two Binary Trees
- 700. Search in a Binary Search Tree
- 98. Validate Binary Search Tree
- 530. Minimum Absolute Difference in BST
- 501. Find Mode in Binary Search Tree
- 236. Lowest Common Ancestor of a Binary Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- 1644. Lowest Common Ancestor of a Binary Tree II
- 1650. Lowest Common Ancestor of a Binary Tree III
- 1676. Lowest Common Ancestor of a Binary Tree IV
- 701. Insert into a Binary Search Tree
- 450. Delete Node in a BST
- 669. Trim a Binary Search Tree
- 108. Convert Sorted Array to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree
- 1008. Construct Binary Search Tree from Preorder Traversal
- 449. Serialize and Deserialize BST
- 538. Convert BST to Greater Tree
- 99. Recover Binary Search Tree
- 103. Binary Tree Zigzag Level Order Traversal
- 1022. Sum of Root To Leaf Binary Numbers
- 116. Populating Next Right Pointers in Each Node
- 117. Populating Next Right Pointers in Each Node II
- 449. Serialize and Deserialize BST
- 297. Serialize and Deserialize Binary Tree
- 652. Find Duplicate Subtrees
- 606. Construct String from Binary Tree
- 124. Binary Tree Maximum Path Sum
- 129. Sum Root to Leaf Numbers
- 988. Smallest String Starting From Leaf
- 230. Kth Smallest Element in a BST
- 671. Second Minimum Node In a Binary Tree
# Basics
1 | |
# Tree
A directional acyclic linear data structure in which each node can point to multiple nodes and only one node can point to self.
# Binary Tree
A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.
# Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent node/internal node has either two or no children.
# Complete Binary Tree
A complete binary tree is a binary tree in which all the levels are completely filled except possibly the lowest one, which is filled from the left.
# Binary Search Tree
A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure whose internal nodes each store a key greater than all the keys in the node’s left subtree and less than those in its right subtree. The time complexity of operations on the binary search tree is directly proportional to the height of the tree.
- pros: No need to adjust the structure when inserting data.
- cons: The performance can be easily affected by the order of data insertion. The worst case is sequential insertion, which degenerates into a linked list.
# Balanced Search Tree
A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.
# AVL Tree
An AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree (BST). It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.
- pros: More balanced compared to binary search tree. The performance is still good enough in extreme cases.
- cons: In the case of a similar read-write ratio, there are too many rotation operations.
# Red–Black Tree
A red–black tree is a kind of self-balancing binary search tree. Each node stores an extra bit representing “color” (“red” or “black”), used to ensure that the tree remains balanced during insertions and deletions.
- Every node has a colour either red or black.
- The root of the tree is always black.
- There are no two adjacent red nodes (A red node cannot have a red parent or red child).
- Every path from a node (including root) to any of its descendants NULL nodes has the same number of black nodes.
- All leaf nodes are black nodes.
- pros: The writing performance is much higher.
- cons: The searching performance is not as good as AVL tree, because the height difference of the child tree is 1 more than AVL, the worst case will have an extra comparison.
# B-Tree
A B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for nodes with more than two children.
M-ary search tree. Used for data indexing, the child node stores pointers and data chunks。the tree structure is maintained by splitting operations.
# B+Tree
A B+ tree is an m-ary tree with a variable but often large number of children per node. A B+ tree consists of a root, internal nodes and leaves. The root may be either a leaf or a node with two or more children.
A B+ tree can be viewed as a B-tree in which each node contains only keys (not key–value pairs), and to which an additional level is added at the bottom with linked leaves.
# B*Tree
B*-tree of order m is a search tree that is either empty or that satisfies three properties:
- The root node has minimum two and maximum 2 floor ((2m-2)/3) +1 children
- Other internal nodes have the minimum floor ((2m-1)/3) and maximum m children
- All external nodes are on the same level.
# LeetCode
# 144. Binary Tree Preorder Traversal
Recursion
1 | |
Iteration
1 | |
# 589. N-ary Tree Preorder Traversal
1 | |
# 145. Binary Tree Postorder Traversal
Recursion
1 | |
Iteration
1 | |
# 590. N-ary Tree Postorder Traversal
1 | |
# 94. Binary Tree Inorder Traversal
Recursion
1 | |
Iteration
1 | |
# 102. Binary Tree Level Order Traversal
1 | |
# 107. Binary Tree Level Order Traversal II
1 | |
Or
1 | |
# 199. Binary Tree Right Side View
1 | |
# 637. Average of Levels in Binary Tree
1 | |
# 429. N-ary Tree Level Order Traversal
1 | |
# 114. Flatten Binary Tree to Linked List
1 | |
# 226. Invert Binary Tree
Recursion
1 | |
Iteration
1 | |
Level Order
1 | |
# 101. Symmetric Tree
Recursion
1 | |
Iteration
1 | |
# 104. Maximum Depth of Binary Tree
Recursion
1 | |
Iteration
1 | |
# 559. Maximum Depth of N-ary Tree
Recursion
1 | |
Iteration
1 | |
# 111. Minimum Depth of Binary Tree
Recursion
1 | |
Iteration
1 | |
# 222. Count Complete Tree Nodes
Recursion
1 | |
Iteration
1 | |
# 110. Balanced Binary Tree
1 | |
# 257. Binary Tree Paths
Simple preorder traversal.
1 | |
# 100. Same Tree
Recursion
1 | |
Iteration
1 | |
# 404. Sum of Left Leaves
1 | |
# 513. Find Bottom Left Tree Value
Layer Order Traversal
1 | |
# 112. Path Sum
Recursion
1 | |
# 113. Path Sum II
Recursion
1 | |
# 437. Path Sum III
1 | |
# 106. Construct Binary Tree from Inorder and Postorder Traversal
1 | |
# 105. Construct Binary Tree from Preorder and Inorder Traversal
1 | |
More concise using slice.
1 | |
# 654. Maximum Binary Tree
Recursion
1 | |
Monotonic Stack
1 | |
# 617. Merge Two Binary Trees
1 | |
# 700. Search in a Binary Search Tree
Recursion
1 | |
Iteration
1 | |
# 98. Validate Binary Search Tree
1 | |
# 530. Minimum Absolute Difference in BST
Inorder Traversal
1 | |
Interval
1 | |
# 501. Find Mode in Binary Search Tree
Inorder Traversal
1 | |
Hash Table
But with extra space.
# 236. Lowest Common Ancestor of a Binary Tree
1 | |
# 235. Lowest Common Ancestor of a Binary Search Tree
1 | |
# 1644. Lowest Common Ancestor of a Binary Tree II
This similar to 236, except we have to
1 | |
# 1650. Lowest Common Ancestor of a Binary Tree III
The problem is equivalent to 160. Using a hash map to stop early.
1 | |
# 1676. Lowest Common Ancestor of a Binary Tree IV
1 | |
1 | |
# 701. Insert into a Binary Search Tree
1 | |
# 450. Delete Node in a BST
- Cannot find in tree.
- If the node is leaf, just delete it. parent.left = null
- If the right child is null, parent.left = node.left
- If the left child is null, parent.left = node.right
- If both the left and right child are not null, there two methods:
- substitute with the least node in right sub-tree, this node is still larger than any node in the left sub-tree. How to find that node? This node is the most left node of right sub-tree.
- hang the left sub-tree to the most left node of right sub-tree, this is equivalent to 3.
1 | |
# 669. Trim a Binary Search Tree
1 | |
# 108. Convert Sorted Array to Binary Search Tree
Keep dividing the num array into two equal parts.
1 | |
# 109. Convert Sorted List to Binary Search Tree
108
Converted to 108. And the time complexity is O(N) and the space complexity is O(N).
Divide Conquer
TC O(N) SC O(logN)
1 | |
# 1008. Construct Binary Search Tree from Preorder Traversal
1 | |
# 449. Serialize and Deserialize BST
Use preorder traversal
1 | |
# 538. Convert BST to Greater Tree
Simply use inverse inorder traversal.
1 | |
# 99. Recover Binary Search Tree
The inorder traversal of binary search tree is monotonically increasing. Since there are only two nodes were swapped, they must be adjacent elements. Find out these two elements and swap them.
1 | |
# 103. Binary Tree Zigzag Level Order Traversal
Use a new queue to store the nodes of the next layer.
1 | |
# 1022. Sum of Root To Leaf Binary Numbers
Using <<.
1 | |
# 116. Populating Next Right Pointers in Each Node
Level Order Traversal
1 | |
O(1) Space
1 | |
# 117. Populating Next Right Pointers in Each Node II
Level Order Traversal Same as 116.
O(1) Space
1 | |
# 449. Serialize and Deserialize BST
Borrow from 144 and 1008.
Be careful with make([]int, 0, 0), this will return a []int{0}
1 | |
# 297. Serialize and Deserialize Binary Tree
Using marshal & unmarshal.
1 | |
# 652. Find Duplicate Subtrees
Hash Map Of Serialized String
Time Complexity O(N^2)
Go through all the letters of that string. Each letter of this string is one node. Therefore, the first solution has a complexity of n ^ 2.
1 | |
Cache the subtree structure
Time complexity decreased to O(N)
1 | |
# 606. Construct String from Binary Tree
1 | |
# 124. Binary Tree Maximum Path Sum
Here’s the situations:
- If there’s only 1 node, return it’s value.
- If there’s 2 nodes, return the sum of all non-negative nodes.
- If there’s 1 root with two child, the maximum path sum can be the left gain + right gain + root.Val. And the max gain can be root.Val + max(left gain, right gain)
- If the subtree’s gain is negative, there’s no need to consider it.
- The null is considered as 0 gain.
1 | |
# 129. Sum Root to Leaf Numbers
Simple dfs problem.
1 | |
# 988. Smallest String Starting From Leaf
Go support lexicographical comparison using >, < operations.
Top-Down
1 | |
# 230. Kth Smallest Element in a BST
Use inorder traversal.
1 | |
1 | |
# 671. Second Minimum Node In a Binary Tree
Divide and conquer
1 | |