LeetCode
LeetCode copied to clipboard
Data Structure (数据结构)
Queue (队列)
Queue是线性表的一种,在操作数据元素时,按照先进先出(First In First Out, FIFO)的规则。
Queue的实现方式: 队列的实现同样有两种方式:顺序存储和链式存储。两者的区别在于数据元素在物理存储结构上的不同。
- 顺序存储
- 链式存储
1. 顺序存储
顺序存储存在的问题: 由于数组申请的空间有限,到某一时间点,就会出现rear队尾指针到了数组的最后一个存储位置,如果继续存储,由于rear指针无法后移,则会出错。
在数组中做删除数据元素的操作,只是移动了队头指针而没有释放所占空间。
顺序存储的升级版:使用数组存取数据元素时,可以将数组申请的空间想象成首尾连接的环状空间使用。
如何区分空队列和满队列的情况?办法是:牺牲掉一个存储空间
- 当队列为空时,尾指针
rear等于头指针front - 当队列为满时,为指针
rear的下一个位置等于头指针front
2. 链式存储
队列的链式存储是在链表的基础上,按照“先进先出”的原则操作数据元素。
Reference
- 队列(Queue):“先进先出”的数据结构
- 链表
- https://www.falkhausen.de/Java-8/java.util/Collection-Hierarchy.html
PriorityQueue (Heap, 堆)
Stack
- 20. Valid Parentheses
- 71. Simplify Path
- 20 & 71都是比较简单、直接的Stack问题。
- 42. Trapping Rain Water
- 84. Largest Rectangle in Histogram
- 85. Maximal Rectangle
- 42,84,85可以用相同的思路解决
- 880. Decoded String at Index
- 2030. Smallest K-Length Subsequence With Occurrences of a Letter
- 316. Remove Duplicate Letters
- This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
- 1249. Minimum Remove to Make Valid Parentheses
- 2751. Robot Collisions
- Stack -- Basic Calculator
- 227. Basic Calculator II [
+, -, *, /] - 224. Basic Calculator [非常好的Stack题目, with parenthesis,
+, -and(, )] - 772. Basic Calculator III [With parenthesis,
+, -, *, /and(, )] - Basic Calculator Template
- 227. Basic Calculator II [
- 84. Largest Rectangle in Histogram
- 394. Decode String
- 735. Asteroid Collision
- 234. Palindrome Linked List
- 456. 132 Pattern
- 155. Min Stack
- 1472. Design Browser History
- 2030. Smallest K-Length Subsequence With Occurrences of a Letter
- 143. Reorder List
- 1762. Buildings With an Ocean View
- 234. Palindrome Linked List
- 844. Backspace String Compare
- 739. Daily Temperatures
Monotonic Stack
https://leetcode.com/tag/monotonic-stack/ A monotonic stack is simply a stack where the elements are always in sorted order. A monotonic stack is a stack in which the elements are always sorted. A stack can be monotonically increasing (sorted ascending) or monotonically decreasing (sorted descending).
LinkedList
Tree
- Binary Tree Traversal
- 314. Binary Tree Vertical Order Traversal
- 102. Binary Tree Level Order Traversal
- How to traverse a tree? https://leetcode.com/problems/binary-tree-level-order-traversal/editorial/
- Lowest Common Ancestor (最近公共祖先)
- 235. Lowest Common Ancestor of a Binary Search Tree
- 236. Lowest Common Ancestor of a Binary Tree
- 1644. Lowest Common Ancestor of a Binary Tree II
- 1650. Lowest Common Ancestor of a Binary Tree III [Two Pointers]
- 160. Intersection of Two Linked Lists [Two Pointers]
- 1676. Lowest Common Ancestor of a Binary Tree IV [DFS+Set]
- Path Sum
- 112. Path Sum
- 113. Path Sum II
- 437. Path Sum III
- Prefix Sum: https://leetcode.com/problems/path-sum-iii/editorial/
- Recursive preorder traversal: https://leetcode.com/problems/sum-root-to-leaf-numbers/editorial/
- 666. Path Sum IV
- 1361. Validate Binary Tree Nodes
- 515. Find Largest Value in Each Tree Row [Level traversal]
Others
Depth
- :white_check_mark: 104. Maximum Depth of Binary Tree, [DFS is better,因为要traverse到最底层的leaf]
- :white_check_mark: 111. Minimum Depth of Binary Tree [BFS is better, 因为只要遇到最早的leaf,就可以提前返回]
- 在LC 104中,要求解的是最大depth,要遍历所有的path才能知道最大的depth,因此DFS是更好的求解思路。
- 而在LC 111中,要求解的是最小depth,只要逐层遍历,遇到最早的叶子节点时,就是遇到了最小的depth,因此用BFS逐层遍历会更好。
- 110. Balanced Binary Tree
- LC 110其实就是在LC 104的基础上的变形。用DFS + early return
- 559. Maximum Depth of N-ary Tree
- LC 559就是在LC 104的基础上把两个子节点扩展成了多个子节点。
- ⚠️ DFS的终止条件!!!
Others
- 894. All Possible Full Binary Trees
- Recursion + Memorization, 有点像Fibonacci
1. 二叉树(BT)遍历
以下前四种traversal都有recursion和iteration两种写法
- 144. Binary Tree Preorder Traversal [DFS]
- 94. Binary Tree Inorder Traversal [DFS]
- 145. Binary Tree Postorder Traversal [DFS]
- 102. Binary Tree Level Order Traversal [BFS]
- 314. Binary Tree Vertical Order Traversal [DFS, BFS]
2. 验证二叉搜索树(BST)
- 98. Validate Binary Search Tree
- 94. Binary Tree Inorder Traversal
- 501. Find Mode in Binary Search Tree
3. 二叉树&二叉搜索树的最近公共祖先
- :exclamation:236. Lowest Common Ancestor of a Binary Tree [
porqcan benull]- 235. Lowest Common Ancestor of a Binary Search Tree [BST的特点:
left.val < root.val < right.val]
- 235. Lowest Common Ancestor of a Binary Search Tree [BST的特点:
- :exclamation:1644. Lowest Common Ancestor of a Binary Tree II [
porqcannot benull] - :exclamation:1650. Lowest Common Ancestor of a Binary Tree III [Both
pandqexist. Two Pointers] - 1676. Lowest Common Ancestor of a Binary Tree IV