Leetcode 0144. Binary Tree Preorder Traversal
144. Binary Tree Preorder TraversalGiven the root of a binary tree, return the preorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,2,3] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [1,2,4,5,6,7,3,8,9] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] 题目大意给定一棵二叉树的根节点 root,返回其节点值的前序遍历结果。前序遍历的顺序是「根节点 → 左子树 → 右子树」,遵循 "根 - 左 - 右" 的递归逻辑。 解题思路前序遍历的...
Leetcode 0142. Linked List Cycle II
142. Linked List Cycle IIGiven the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter. Do not modify the linked list. Examp...
Leetcode 0131. Palindrome Partitioning
131. Palindrome PartitioningGiven a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example 1: 12Input: s = "aab"Output: [["a","a","b"],["aa","b"]] Example 2: 12Input: s = "a"Output: [["a"]] 题目大意给定一个字符串 s,将其分割成若干个子串,使得每个子串都是回文串。返回所有可能的分割方案。 例如: 输入 s = "aab",输出 [["a","a","b"],["aa&quo...
Leetcode 0116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each NodeYou 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: 123456struct Node { int val; Node *left; Node *right; Node *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. Example 1: 123Input: root = [1...
Leetcode 0112. Path Sum
112. Path SumGiven 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. Example 1: 123Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22Output: trueExplanation: The root-to-leaf path with the target sum is shown. Example 2: 123456Input: root = [1,2,3], targetSum = 5Output: falseExplanation: There are two root-to-leaf pat...
Leetcode 0110. Balanced Binary Tree
110. Balanced Binary TreeGiven a binary tree, determine if it is height-balanced. Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: true Example 2: 12Input: root = [1,2,2,3,3,null,null,4,4]Output: false Example 3: 12Input: root = []Output: true 题目大意给定一棵二叉树,判断它是否是高度平衡的。高度平衡平衡二叉树 ** 的定义是:二叉树的每个节点的左右两个子树的高度差的绝对值不超过 1。 例如: 输入二叉树 [3,9,20,null,null,15,7],每个节点的左右子树高度差均不超过 1,返回 true; 输入二叉树 [1,2,2,3,3,null,null,4,4],根节点的左子树高度为 3,右子树高度为 1,差为 2,返回 false。 解题思路判断平衡二叉树的核心是计算每个节点的左右子树高度,并检查高...
Leetcode 0108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search TreeGiven an integer array nums where the elements are sorted in ascending order, convert it to a *height-balanced* binary search tree. Example 1: 123Input: nums = [-10,-3,0,5,9]Output: [0,-3,9,-10,null,5]Explanation: [0,-10,5,null,-3,null,9] is also accepted: Example 2: 123Input: nums = [1,3]Output: [3,1]Explanation: [1,null,3] and [3,1] are both height-balanced BSTs. 题目大意给定一个升序排列的整数数组 nums,将其转换为一棵高度平衡的二叉搜索树(BST)。高度平衡的定义是:二叉树的左右两个子树的高度差的绝对值不超过 ...
Leetcode 0107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal IIGiven the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[15,7],[9,20],[3]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,返回其节点值的自底向上的层序遍历结果。即按「从叶子节点所在层到根节点所在层」的顺序逐层返回,每层内部仍保持「从左到右」的顺序。 例如: 输入二叉树 [3,9,20,null,null,15,7],其自底向上...
Leetcode 0106. Construct Binary Tree from Inorder and Postorder Traversal
106. Construct Binary Tree from Inorder and Postorder TraversalGiven 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. Example 1: 12Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]Output: [3,9,20,null,null,15,7] Example 2: 12Input: inorder = [-1], postorder = [-1]Output: [-1] 题目大意给定一棵二叉树的中序遍历数组 inorder 和后序遍历数组 postorder,请构造并返回这棵二叉树。 例如: ...
Leetcode 0105. Construct Binary Tree from Preorder and Inorder Traversal
105. Construct Binary Tree from Preorder and Inorder Traversal题目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 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / \ 9 20 / \ 15 7 题目大意根据一棵树的前序遍历与中序遍历构造二叉树。 注意:你可以假设树中没有重复的元素。 解题思路 inorder_map:用于快速查找中序遍历中元素对应的索引,时间复杂度 O (1) pre:保存前序遍历数组的副本,避免在递归中反复传递参数 确定根节点:前序遍历的第一个元素为当前树的根节点 划分左右...
Leetcode 0104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary TreeGiven 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. Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: 3 Example 2: 12Input: root = [1,null,2]Output: 2 题目大意给定一棵二叉树的根节点 root,返回该二叉树的最大深度。二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数量。 例如: 输入二叉树 [3,9,20,null,null,15,7],其最大深度为 3(路径:3 → 20 → 15 或 3 → 20 → 7,均包含 3 个节点)。 解题思路计算二叉树的最...
Leetcode 0103. Binary Tree Zigzag Level Order Traversal
103. Binary Tree Zigzag Level Order Traversal 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). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[20,9],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点,返回其节点值的锯齿形层序遍历。即:第一层从左到右,第二层从右到左,第三层再从左到右,以此类推,交替进行。 例如: 输入 root = [3,9,20,null...
Leetcode 0102. Binary Tree Level Order Traversal
102. Binary Tree Level Order TraversalGiven the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). Example 1: 12Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]] Example 2: 12Input: root = [1]Output: [[1]] Example 3: 12Input: root = []Output: [] 题目大意给定一棵二叉树的根节点 root,返回其节点值的层序遍历结果(即从左到右、逐层遍历)。结果需以二维数组形式呈现,每一层的节点值构成一个子数组(例如,第一层 [根节点],第二层 [左子节点, 右子节点],以此类推)。 解题思路层序遍历的核心是按照树的层次依次访问节点,从根节点开始,先访问第一层(根节点)...
Leetcode 0101. Symmetric Tree
101. Symmetric TreeGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example 1: 12Input: root = [1,2,2,3,4,4,3]Output: true Example 2: 12Input: root = [1,2,2,null,3,null,3]Output: false 题目大意给定一棵二叉树的根节点 root,判断该二叉树是否是对称的(即围绕中心轴镜像对称)。 例如: 输入二叉树 [1,2,2,3,4,4,3],其左子树与右子树成镜像,故返回 true; 输入二叉树 [1,2,2,null,3,null,3],左子树与右子树不镜像,故返回 false。 解题思路判断二叉树是否对称的核心是比较左子树与右子树是否成镜像,即: 左子树的左节点需与右子树的右节点值相等; 左子树的右节点需与右子树的左节点值相等。 1. 递归法设计一个辅助函数,比较两个子树...
Leetcode 0100. Same Tree
100. Same TreeGiven 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: 12Input: p = [1,2,3], q = [1,2,3]Output: true Example 2: 12Input: p = [1,2], q = [1,null,2]Output: false Example 3: 12Input: p = [1,2,1], q = [1,1,2]Output: false 题目大意给定两棵二叉树的根节点 p 和 q,判断这两棵树是否相同。两棵树相同的定义是:结构完全相同,且对应节点的值也相同。 例如: 输入两棵结构和节点值均相同的树 [1,2,3]...
Leetcode 0099. Recover Binary Search Tree
99. Recover Binary Search TreeYou are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Example 1: 123Input: root = [1,3,null,null,2]Output: [3,1,null,null,2]Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid. Example 2: 123Input: root = [3,1,4,null,null,2]Output: [2,1,4,null,null,3]Explanation: 2 cannot be in the right subt...
Leetcode 0098. Validate Binary Search Tree
98. Validate Binary Search TreeGiven 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 strictly less than the node's key. The right subtree of a node contains only nodes with keys strictly greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: 12Input: root = [2,1,3]Output: true Example 2: 123Input: root = [...
Leetcode 0096. Unique Binary Search Trees
96. Unique Binary Search TreesGiven an integer n, return *the number of structurally unique **BST'*s (binary search trees) which has exactly n nodes of unique values from 1 to n. Example 1: 12Input: n = 3Output: 5 Example 2: 12Input: n = 1Output: 1 题目大意给定一个整数 n,计算由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)的数量。 例如: 输入 n = 3,存在 5 种结构独特的 BST,返回 5; 输入 n = 1,只有 1 种 BST,返回 1。 解题思路计算独特 BST 的数量可以通过动态规划(DP) 实现,核心思路基于以下观察: 若选择 i 作为根节点,则左子树由 1~i-1 构成,右子树由 i+1~n 构成; 左子树的独特结构数量为 dp[i-1],右子树的独特结构数量为 dp[n-i](...
Leetcode 0095. Unique Binary Search Trees II
95. Unique Binary Search Trees IIGiven an integer n, return *all the structurally unique **BST'*s (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order. Example 1: 12Input: n = 3Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] Example 2: 12Input: n = 1Output: [[1]] 题目大意给定一个整数 n,生成所有由 1 到 n 为节点所组成的结构独特的二叉搜索树(BST)。返回这些二叉搜索树的根节点列表。 二叉搜索树的特性是:对于任意节点,其左子树中的所有节点值都小于该节点值,右子树中的所有节点值都大于该节点值。 例如: 输入 n = 3,存在...
Leetcode 0094. Binary Tree Inorder Traversal
94. Binary Tree Inorder TraversalGiven the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] 题目大意 给定一棵二叉树的根节点 root,返回其节点值的中序遍历结果。中序遍历的顺序是「左子树 → 根节点 → 右子树」,遵循 “左 - 根 - 右” 的递归逻辑,且需按此顺序收集所有节点值。 解题思路二叉...
Leetcode 0093. Restore IP Addresses
93. Restore IP AddressesA valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros. For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses. Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots ...
Leetcode 0090. Subsets II
90. Subsets IIGiven an integer array nums that may contain duplicates, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: 12Input: nums = [1,2,2]Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: 12Input: nums = [0]Output: [[],[0]] 题目大意给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(即幂集)。幂集需包含所有可能的子集(包括空集和数组本身),且不能有重复子集,结果顺序可任意。 例如: 输入 nums = [1,2,2],输出 [[],[1],[1,2],[1,2,2],[2],[2,2]](共 6 个子集,无重复); 输入 nums =...
Leetcode 0086. Partition List
86. Partition ListGiven the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example 1: 12Input: head = [1,4,3,2,5,2], x = 3Output: [1,2,2,4,3,5] Example 2: 12Input: head = [2,1], x = 2Output: [1,2] 题目大意给定链表的头节点 head 和一个值 x,要求将链表分隔成两部分:所有值小于 x 的节点排在所有值大于或等于 x 的节点之前。同时需要保留两部分中节点的原始相对顺序。 解题思路可以通过构建两个临时链表来解决: 创建两个虚拟头节点,分别用于...
Leetcode 0084. 柱状图中最大的矩形
84. 柱状图中最大的矩形给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。 求在该柱状图中,能够勾勒出来的矩形的最大面积。 示例 1: 123输入:heights = [2,1,5,6,2,3]输出:10解释:最大的矩形为图中红色区域,面积为 10 示例 2: 12输入: heights = [2,4]输出: 4 解题思路该解法是对单调栈思路的优化实现,通过在数组末尾添加哨兵元素和栈初始化技巧,将左右边界的计算合并到一次遍历中,代码更简洁且效率更高。 核心优化思路 哨兵元素(Sentinel): 在原数组末尾添加 -1(高度为负数的哨兵),确保遍历结束时栈中所有元素都会被弹出计算(相当于 “大火收汁”); 栈初始时推入 -1(索引哨兵),解决栈空时的边界判断问题,同时自然对应 “左侧无更矮柱子” 的情况(left[i] = -1)。 一次遍历计算左右边界: 遍历每个元素作为右侧边界(right),当当前高度小于栈顶高度时,栈顶元素的左右边界均已确定: 右边界:当前索引 right(第一个比栈顶元素矮的位置); 左边界:弹出栈...
Leetcode 0082. Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List IIGiven the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: 12Input: head = [1,2,3,3,4,4,5]Output: [1,2,5] Example 2: 12Input: head = [1,1,1,2,3]Output: [2,3] 题目大意给定一个已排序的链表,要求删除所有存在重复的节点(即重复的节点一个都不保留),仅保留原链表中只出现过一次的节点,最终返回排序后的新链表头节点。 核心解题思路由于链表已排序,重复节点必然相邻,因此可通过「遍历链表 + 跳过重复节点」的思路解决,关键是: 虚拟头节点:避免删除头节点时的特殊处理(如示例 2 中头节点 ...

