\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{woshiamiaojiang} \pdfinfo{ /Title (leetcode.pdf) /Creator (Cheatography) /Author (woshiamiaojiang) /Subject (Leetcode Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Leetcode Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{woshiamiaojiang} via \textcolor{DarkBackground}{\uline{cheatography.com/193748/cs/40367/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}woshiamiaojiang \\ \uline{cheatography.com/woshiamiaojiang} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 20th September, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{340. 至多包含 K 个不同字符}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{样例1: \newline 输入:S="eceba"并且k=3 \newline 输出:4 \newline 解释:T="eceb" \newline 样例2: \newline 输入:S="WORLD"并且k=4 \newline 输出:4 \newline 解释:T="WORL"或"ORLD" \newline \newline \newline public class Solution \{ \newline public int \seqsplit{lengthOfLongestSubstringKDistinct(String} s, int k) \{ \newline \newline if (s.length() == 0 || k == 0) \{ \newline return 0; \newline \} \newline \newline int left = 0, right = 0, cnt = 0; \newline int charSet{[}{]} = new int{[}256{]}; \newline int ans = 0; \newline \newline while (right \textless{} s.length()) \{ \newline // \seqsplit{统计right指向的字符} \newline // \seqsplit{当字符在窗口内第一次出现时,字符种类数+1,该字符出现次数+1} \newline if (charSet{[}s.charAt(right){]} == 0) \{ \newline cnt++; \newline \} \newline charSet{[}s.charAt(right){]}++; \newline right++; \newline \newline // \seqsplit{向右移动left,保持窗口内只有k种不同的字符} \newline while (cnt \textgreater{} k) \{ \newline charSet{[}s.charAt(left){]}-{}-; \newline // \seqsplit{当该字符在本窗口不再出现时,字符种类数-1} \newline if (charSet{[}s.charAt(left){]} == 0) \{ \newline cnt-{}-; \newline \} \newline left++; \newline \} \newline \newline // 更新答案,此时 cnt == k \newline ans = Math.max(ans, right - left); \newline \} \newline return ans; \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{4. \seqsplit{寻找两个正序数组的中位数}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline 输入:nums1 = {[}1,3{]}, nums2 = {[}2{]} \newline 输出:2.00000 \newline 解释:合并数组 = {[}1,2,3{]} ,中位数 2 \newline 示例 2: \newline \newline 输入:nums1 = {[}1,2{]}, nums2 = {[}3,4{]} \newline 输出:2.50000 \newline 解释:合并数组 = {[}1,2,3,4{]} ,中位数 (2 + 3) / 2 = 2.5 \newline \newline \newline \newline class Solution \{ \newline public double findMedianSortedArrays(int{[}{]} nums1, int{[}{]} nums2) \{ \newline int len1 = nums1.length; \newline int len2 = nums2.length; \newline // 减少操作次数 \newline if (len1 \textgreater{} len2) \{ \newline // 确保N1是短的部分 \newline return \seqsplit{findMedianSortedArrays(nums2}, nums1); \newline \} \newline \newline // \seqsplit{开始从nums1中取元素} \newline int left = 0; \newline int right = len1; \newline \newline while (left \textless{}= right) \{ \newline // \seqsplit{从nums1和nums2中分别取m1和m2个元素组成k个元素} \newline int m1 = left + (right - left) / 2; \newline int m2 = (len1 + len2) / 2 - m1; \newline \newline // k -\textgreater{} max(L1, L2), (k + 1) -\textgreater{} min(R1, R2) \newline // nums1没有取元素? \newline // \seqsplit{此时左边的元素一定从nums2中取,因此要使得L1} = MIN\_VALUE \newline // 这样max(L1, L2)就会取到L2了 \newline // \seqsplit{这时所取得元素中num2的元素都要比nums1中的小} \newline // 因此在min(R1,R2)的时候,选中的也是R2 \newline double m1Left = (m1 == 0) ? Integer.MIN\_VALUE : nums1{[}m1 - 1{]}; \newline // \seqsplit{nums1取了所有的元素}? \newline double m1Right = (m1 == len1) ? Integer.MAX\_VALUE : nums1{[}m1{]}; \newline \newline // nums2没有取元素? \newline double m2Left = (m2 == 0) ? Integer.MIN\_VALUE : nums2{[}m2 - 1{]}; \newline // \seqsplit{nums2取了所有的元素}? \newline double m2Right = (m2 == len2) ? Integer.MAX\_VALUE : nums2{[}m2{]}; \newline \newline // 左边取多了 \newline if (m1Left \textgreater{} m2Right) \{ \newline right = m1 - 1; \newline \}else if (m2Left \textgreater{} m1Right) \{ \newline // 左边取少了 \newline left = m1 + 1; \newline \} else \{ \newline // \seqsplit{此时是符合要求的排列情况} \newline if ((len1 + len2) \% 2 == 0)\{ \newline double medLeft = Math.max(m1Left, m2Left); \newline double medRight = Math.min(m1Right, m2Right); \newline return (medLeft + medRight) / 2.0; \newline \} else \{ \newline return Math.min(m1Right, m2Right); \newline \} \newline \} \newline \} \newline \newline return -1; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{662. 二叉树最大宽度}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给你一棵二叉树的根节点} root ,返回树的 最大宽度 。 \newline \newline 树的 最大宽度 是所有层中最大的 宽度 。 \newline \newline 每一层的 宽度 \seqsplit{被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的} null 节点,这些 null 节点也计入长度。 \newline \newline 题目数据保证答案将会在 32 位 \seqsplit{带符号整数范围内。} \newline \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}1,3,2,5,3,null,9{]} \newline 输出:4 \newline 解释:最大宽度出现在树的第 3 层,宽度为 4 (5,3,null,9) 。 \newline 示例 2: \newline \newline \newline 输入:root = {[}1,3,2,5,null,null,9,6,null,7{]} \newline 输出:7 \newline 解释:最大宽度出现在树的第 4 层,宽度为 7 (6,null,null,null,null,null,7) 。 \newline 示例 3: \newline \newline \newline 输入:root = {[}1,3,2,5{]} \newline 输出:2 \newline 解释:最大宽度出现在树的第 2 层,宽度为 2 (3,2) 。 \newline 此题求二叉树所有层的最大宽度,比较直观的方法是求出每一层的宽度,然后 \newline 求出最大值。求每一层的宽度时,因为两端点间的nul1节点也需要计入宽 \newline 度,因此可以对节点进行编号。一个编号为index的左子节点的编号记为 \newline 2xindex,右子节点的编号记为2×index+1,计算每层宽度时,用每层节 \newline 点的最大编号减去最小编号再加1即为宽度。 \newline 遍历节点时,可以用广度优先搜索来遍历每一层的节点,并求出最大值。 \newline \newline \newline \newline class Solution \{ \newline public int \seqsplit{widthOfBinaryTree(TreeNode} root) \{ \newline int res = 1; \newline List\textless{}Pair\textless{}TreeNode, Integer\textgreater{}\textgreater{} arr = new ArrayList\textless{}Pair\textless{}TreeNode, Integer\textgreater{}\textgreater{}(); \newline arr.add(new Pair\textless{}TreeNode, Integer\textgreater{}(root, 1)); \newline while (!arr.isEmpty()) \{ \newline List\textless{}Pair\textless{}TreeNode, Integer\textgreater{}\textgreater{} tmp = new ArrayList\textless{}Pair\textless{}TreeNode, Integer\textgreater{}\textgreater{}(); \newline for (Pair\textless{}TreeNode, Integer\textgreater{} pair : arr) \{ \newline TreeNode node = pair.getKey(); \newline int index = pair.getValue(); \newline if (node.left != null) \{ \newline tmp.add(new Pair\textless{}TreeNode, Integer\textgreater{}(node.left, index {\emph{ 2)); \newline \} \newline if (node.right != null) \{ \newline tmp.add(new Pair\textless{}TreeNode, Integer\textgreater{}(node.right, index }} 2 + 1)); \newline \} \newline \} \newline res = Math.max(res, arr.get(arr.size() - 1).getValue() - arr.get(0).getValue() + 1); \newline arr = tmp; \newline \} \newline return res; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{236. \seqsplit{二叉树的最近公共祖先}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{给定一个二叉树, \seqsplit{找到该树中两个指定节点的最近公共祖先。} \newline \newline 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个节点 \seqsplit{p、q,最近公共祖先表示为一个节点} x,满足 x 是 p、q 的祖先且 x \seqsplit{的深度尽可能大(一个节点也可以是它自己的祖先)。"} \newline \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}3,5,1,6,2,0,8,null,null,7,4{]}, p = 5, q = 1 \newline 输出:3 \newline 解释:节点 5 和节点 1 \seqsplit{的最近公共祖先是节点} 3 。 \newline 示例 2: \newline \newline \newline 输入:root = {[}3,5,1,6,2,0,8,null,null,7,4{]}, p = 5, q = 4 \newline 输出:5 \newline 解释:节点 5 和节点 4 \seqsplit{的最近公共祖先是节点} 5 \seqsplit{。因为根据定义最近公共祖先节点可以为节点本身。} \newline 示例 3: \newline \newline 输入:root = {[}1,2{]}, p = 1, q = 2 \newline 输出:1 \newline \newline \newline class Solution \{ \newline public TreeNode \seqsplit{lowestCommonAncestor(TreeNode} root, TreeNode p, TreeNode q) \{ \newline // base case \newline if (root == null) return null; \newline if (root == p || root == q) return root; \newline \newline TreeNode left = \seqsplit{lowestCommonAncestor(root}.left, p, q); \newline TreeNode right = \seqsplit{lowestCommonAncestor(root}.right, p, q); \newline // 情况 1 \newline if (left != null \&\& right != null) \{ \newline return root; \newline \} \newline // 情况 2 \newline if (left == null \&\& right == null) \{ \newline return null; \newline \} \newline // 情况 3 \newline return left == null ? right : left; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{25. K 个一组翻转链表}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{给你链表的头节点 head ,每 k \seqsplit{个节点一组进行翻转,请你返回修改后的链表。} \newline \newline k \seqsplit{是一个正整数,它的值小于或等于链表的长度。如果节点总数不是} k \seqsplit{的整数倍,那么请将最后剩余的节点保持原有顺序。} \newline \newline 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。 \newline \newline \newline 示例 1: \newline \newline 输入:head = {[}1,2,3,4,5{]}, k = 2 \newline 输出:{[}2,1,4,3,5{]} \newline 示例 2: \newline \newline \newline 输入:head = {[}1,2,3,4,5{]}, k = 3 \newline 输出:{[}3,2,1,4,5{]} \newline \newline \newline class Solution \{ \newline public ListNode reverseKGroup(ListNode head, int k) \{ \newline if (head == null) return null; \newline // 区间 {[}a, b) 包含 k 个待反转元素 \newline ListNode a, b; \newline a = b = head; \newline for (int i = 0; i \textless{} k; i++) \{ \newline // 不足 k \seqsplit{个,不需要反转,base} case \newline if (b == null) return head; \newline b = b.next; \newline \} \newline // 反转前 k 个元素 \newline ListNode newHead = reverse(a, b); \newline // \seqsplit{递归反转后续链表并连接起来} \newline a.next = reverseKGroup(b, k); \newline \newline return newHead; \newline \} \newline \newline /{\emph{ 反转区间 {[}a, b) \seqsplit{的元素,注意是左闭右开} }}/ \newline ListNode reverse(ListNode a, ListNode b) \{ \newline \newline ListNode pre, cur, nxt; \newline pre = null; \newline cur = a; \newline nxt = a; \newline // while \seqsplit{终止的条件改一下就行了} \newline while (cur != b) \{ \newline nxt = cur.next; \newline cur.next = pre; \newline pre = cur; \newline cur = nxt; \newline \} \newline // \seqsplit{返回反转后的头结点} \newline return pre; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{95. 不同的二叉搜索树 II}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{给你一个整数 n \seqsplit{,请你生成并返回所有由} n \seqsplit{个节点组成且节点值从} 1 到 n 互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。 \newline \newline \newline 示例 1: \newline \newline 输入:n = 3 \newline 输出:{[}{[}1,null,2,null,3{]},{[}1,null,3,2{]},{[}2,1,3{]},{[}3,1,null,null,2{]},{[}3,2,null,1{]}{]} \newline 示例 2: \newline \newline 输入:n = 1 \newline 输出:{[}{[}1{]}{]} \newline \newline \newline class Solution \{ \newline public List\textless{}TreeNode\textgreater{} generateTrees(int n) \{ \newline if (n == 0) \{ \newline return new LinkedList\textless{}TreeNode\textgreater{}(); \newline \} \newline return generateTrees(1, n); \newline \} \newline \newline public List\textless{}TreeNode\textgreater{} generateTrees(int start, int end) \{ \newline List\textless{}TreeNode\textgreater{} allTrees = new LinkedList\textless{}TreeNode\textgreater{}(); \newline if (start \textgreater{} end) \{ \newline allTrees.add(null); \newline return allTrees; \newline \} \newline \newline // 枚举可行根节点 \newline for (int i = start; i \textless{}= end; i++) \{ \newline // \seqsplit{获得所有可行的左子树集合} \newline List\textless{}TreeNode\textgreater{} leftTrees = generateTrees(start, i - 1); \newline \newline // \seqsplit{获得所有可行的右子树集合} \newline List\textless{}TreeNode\textgreater{} rightTrees = generateTrees(i + 1, end); \newline \newline // \seqsplit{从左子树集合中选出一棵左子树,从右子树集合中选出一棵右子树,拼接到根节点上} \newline for (TreeNode left : leftTrees) \{ \newline for (TreeNode right : rightTrees) \{ \newline TreeNode currTree = new TreeNode(i); \newline currTree.left = left; \newline currTree.right = right; \newline allTrees.add(currTree); \newline \} \newline \} \newline \} \newline return allTrees; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{103. \seqsplit{二叉树的锯齿形层次}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{103. \seqsplit{二叉树的锯齿形层序遍历} \newline \newline 给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 \seqsplit{。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。} \newline \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}3,9,20,null,null,15,7{]} \newline 输出:{[}{[}3{]},{[}20,9{]},{[}15,7{]}{]} \newline 示例 2: \newline \newline 输入:root = {[}1{]} \newline 输出:{[}{[}1{]}{]} \newline 示例 3: \newline \newline 输入:root = {[}{]} \newline 输出:{[}{]} \newline \newline \newline class Solution \{ \newline public List\textless{}List\textless{}Integer\textgreater{}\textgreater{} \seqsplit{zigzagLevelOrder(TreeNode} root) \{ \newline // \seqsplit{定义需要返回的变量} \newline List\textless{}List\textless{}Integer\textgreater{}\textgreater{} res = new ArrayList\textless{}\textgreater{}(); \newline // 创建双端队列 \newline Queue\textless{}TreeNode\textgreater{} queue = new ArrayDeque\textless{}\textgreater{}(); \newline // 创建层数索引 \newline int idx = 0; \newline // 如果节点不为空 \newline if (root != null) \{ \newline queue.add(root); \newline \} \newline // 如果队列不为空 \newline while (!queue.isEmpty()) \{ \newline // \seqsplit{计算这一层的根节点数} \newline int n = queue.size(); \newline idx++; \newline // 新建List,用于存储每一层的节点数 \newline List\textless{}Integer\textgreater{} level = new ArrayList\textless{}\textgreater{}(); \newline for (int i = 0; i \textless{} n; i++) \{ \newline TreeNode node = queue.poll(); \newline // 记录当前根节点 \newline level.add(node.val); \newline // \seqsplit{将根节点删除,并依次加入所删除根节点的左节点与右节点。} \newline if (node.left != null) \{ \newline queue.add(node.left); \newline \} \newline if (node.right != null) \{ \newline queue.add(node.right); \newline \} \newline \} \newline // 判断奇偶层 \newline if (idx\%2 == 0)\{ \newline //反转level \newline \seqsplit{Collections.reverse(level);} \newline \} \newline res.add(level); \newline \} \newline // 返回结果 \newline return res; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{700. \seqsplit{二叉搜索树中的搜索}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给定二叉搜索树(BST)的根节点} root 和一个整数值 val。 \newline \newline 你需要在 BST 中找到节点值等于 val 的节点。 \seqsplit{返回以该节点为根的子树。} \seqsplit{如果节点不存在,则返回} null 。 \newline \newline 示例 1: \newline \newline 输入:root = {[}4,2,7,1,3{]}, val = 2 \newline 输出:{[}2,1,3{]} \newline 示例 2: \newline \newline 输入:root = {[}4,2,7,1,3{]}, val = 5 \newline 输出:{[}{]} \newline \newline class Solution \{ \newline public TreeNode searchBST(TreeNode root, int target) \{ \newline if (root == null) \{ \newline return null; \newline \} \newline // 去左子树搜索 \newline if (root.val \textgreater{} target) \{ \newline return searchBST(root.left, target); \newline \} \newline // 去右子树搜索 \newline if (root.val \textless{} target) \{ \newline return searchBST(root.right, target); \newline \} \newline return root; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{1038. \seqsplit{从二叉搜索树到更大和树}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给定一个二叉搜索树} root \seqsplit{(BST),请将它的每个节点的值替换成树中大于或者等于该节点值的所有节点值之和。} \newline \newline 提醒一下, 二叉搜索树 \seqsplit{满足下列约束条件:} \newline \newline 节点的左子树仅包含键 小于 节点键的节点。 \newline 节点的右子树仅包含键 大于 节点键的节点。 \newline 左右子树也必须是二叉搜索树。 \newline \newline \newline 输入:{[}4,1,6,0,2,5,7,null,null,null,3,null,null,null,8{]} \newline 输出:{[}30,36,21,36,35,26,15,null,null,null,33,null,null,null,8{]} \newline 示例 2: \newline \newline 输入:root = {[}0,null,1{]} \newline 输出:{[}1,null,1{]} \newline \newline \newline // 右根左遍历 \newline class Solution \{ \newline \newline int tmp = 0; \newline \newline public TreeNode bstToGst(TreeNode root) \{ \newline traverse(root); \newline return root; \newline \} \newline \newline public void traverse(TreeNode root) \{ \newline if (root == null) return; \newline traverse(root.right); \newline tmp += root.val; \newline root.val = tmp; \newline traverse(root.left); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{20. 有效的括号}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Stack\textless{}Character\textgreater{} stack = new Stack\textless{}\textgreater{}(); \newline for (char c : s.toCharArray()) \{ \newline switch (c) \{ \newline case '(': stack.push(')'); break; \newline case '{[}': stack.push('{]}'); break; \newline case '\{': stack.push('\}'); break; \newline default: if (stack.isEmpty() || c != stack.pop()) return false; \newline \} \newline \} \newline return stack.isEmpty();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{297. \seqsplit{二叉树的序列化与反序列化}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{输入:root = {[}1,2,3,null,null,4,5{]} \newline 输出:{[}1,2,3,null,null,4,5{]} \newline 示例 2: \newline \newline 输入:root = {[}{]} \newline 输出:{[}{]} \newline 示例 3: \newline \newline 输入:root = {[}1{]} \newline 输出:{[}1{]} \newline 示例 4: \newline \newline 输入:root = {[}1,2{]} \newline 输出:{[}1,2{]} \newline \newline public class Codec \{ \newline \newline LinkedList\textless{}String\textgreater{} list = new LinkedList\textless{}\textgreater{}(); \newline \newline public String serialize(TreeNode root) \{ \newline return traverse(root); \newline \} \newline \newline public String traverse(TreeNode root) \{ \newline if (root == null) return "null"; \newline String left = traverse(root.left); \newline String right = traverse(root.right); \newline return root.val + "," + left + "," + right; \newline \} \newline \newline public TreeNode deserialize(String data) \{ \newline String{[}{]} strs = data.split(","); \newline for (String str : strs) \{ \newline list.add(str); \newline \} \newline return traverse(); \newline \} \newline \newline public TreeNode traverse() \{ \newline String str = list.removeFirst(); \newline if ("null".equals(str)) return null; \newline TreeNode root = new \seqsplit{TreeNode(Integer.valueOf(str));} \newline root.left = traverse(); \newline root.right = traverse(); \newline return root; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{114. 二叉树展开为链表}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{输入:root = {[}1,2,5,3,4,null,6{]} \newline 输出:{[}1,null,2,null,3,null,4,null,5,null,6{]} \newline 示例 2: \newline \newline 输入:root = {[}{]} \newline 输出:{[}{]} \newline 示例 3: \newline \newline 输入:root = {[}0{]} \newline 输出:{[}0{]} \newline \newline \newline \newline \newline class Solution \{ \newline // 定义:将以 root \seqsplit{为根的树拉平为链表} \newline public void flatten(TreeNode root) \{ \newline // base case \newline if (root == null) return; \newline // \seqsplit{先递归拉平左右子树} \newline flatten(root.left); \newline flatten(root.right); \newline \newline /{\bf{{\emph{*后序遍历位置}}}}*/ \newline // \seqsplit{1、左右子树已经被拉平成一条链表} \newline TreeNode left = root.left; \newline TreeNode right = root.right; \newline \newline // \seqsplit{2、将左子树作为右子树} \newline root.left = null; \newline root.right = left; \newline \newline // \seqsplit{3、左子树的末端连接上当前的右子树} \newline TreeNode p = root; \newline while (p.right != null) \{ \newline p = p.right; \newline \} \newline p.right = right; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{101. 对称二叉树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline \newline 输入:root = {[}1,2,2,3,4,4,3{]} \newline 输出:true \newline 示例 2: \newline \newline \newline 输入:root = {[}1,2,2,null,3,null,3{]} \newline 输出:false \newline \newline \newline \newline class Solution \{ \newline public boolean isSymmetric(TreeNode root) \{ \newline return isSymmetric(root.left, root.right); \newline \} \newline \newline public boolean isSymmetric(TreeNode left, TreeNode right) \{ \newline if (left == null \&\& right == null) return true; \newline if (left == null || right == null || left.val != right.val) return false; \newline return isSymmetric(left.left, right.right) \&\& isSymmetric(left.right, right.left); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{144. 二叉树的前序遍历}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline \newline List\textless{}Integer\textgreater{} res = new LinkedList\textless{}\textgreater{}(); \newline \newline public List\textless{}Integer\textgreater{} \seqsplit{preorderTraversal(TreeNode} root) \{ \newline traverse(root); \newline return res; \newline \} \newline \newline public void traverse(TreeNode root) \{ \newline if (root == null) \{ \newline return; \newline \} \newline res.add(root.val); \newline traverse(root.left); \newline traverse(root.right); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{111. 二叉树的最小深度}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline public int minDepth(TreeNode root) \{ \newline if (root == null) return 0; \newline LinkedList\textless{}TreeNode\textgreater{} list = new LinkedList\textless{}\textgreater{}(); \newline list.add(root); \newline int depth = 0; \newline while (!list.isEmpty()) \{ \newline int size = list.size(); \newline depth++; \newline for (int i = 0; i \textless{} size; i++) \{ \newline TreeNode cur = list.removeFirst(); // eaily wrong \newline if (cur.left == null \&\& cur.right == null) \{ \newline return depth; \newline \} \newline if (cur.left != null) list.add(cur.left); \newline if (cur.right != null) list.add(cur.right); \newline \} \newline \newline \} \newline return depth; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{105. \seqsplit{从前序与中序遍历序列构造二叉树}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline \newline HashMap\textless{}Integer, Integer\textgreater{} inOrderValMapIdx = new HashMap\textless{}\textgreater{}(); \newline \newline public TreeNode buildTree(int{[}{]} preOrder, int{[}{]} inOrder) \{ \newline for (int i = 0; i \textless{} preOrder.length; i++) inOrderValMapIdx.put(inOrder{[}i{]}, i); \newline return buildTree(preOrder, inOrder, 0, preOrder.length - 1, 0, inOrder.length - 1); \newline \} \newline \newline // pre: 3 9 20 15 7 \newline // T L \newline // in : 9 3 15 20 7 \newline // L T \newline public TreeNode buildTree(int{[}{]} preOrder, int{[}{]} inOrder, int preLeft, int preRight, int inLeft, int inRight) \{ \newline if (preLeft \textgreater{} preRight) return null; \newline int rootVal = preOrder{[}preLeft{]}; \newline TreeNode root = new TreeNode(rootVal); \newline int rootIdx = \seqsplit{inOrderValMapIdx.get(rootVal);} \newline int inLeftSize = rootIdx - inLeft; // 1 \newline root.left = buildTree(preOrder, inOrder, preLeft + 1, preLeft + inLeftSize, inLeft, inLeft + inLeftSize - 1); \newline root.right = buildTree(preOrder, inOrder, preLeft + inLeftSize + 1, preRight, rootIdx + 1, inRight); \newline return root; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{124. \seqsplit{二叉树中的最大路径和}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline \newline int res = Integer.MIN\_VALUE; \newline \newline public int maxPathSum(TreeNode root) \{ \newline getOneSideMax(root); \newline return res; \newline \} \newline \newline public int getOneSideMax(TreeNode root) \{ \newline if (root == null) \{ \newline return 0; \newline \} \newline int left = Math.max(0, \seqsplit{getOneSideMax(root.left));} \newline int right = Math.max(0, \seqsplit{getOneSideMax(root.right));} \newline res = Math.max(left + right + root.val, res); \newline return Math.max(left, right) + root.val; \newline \} \newline \} \newline \newline \newline 543. 二叉树的直径 \newline class Solution \{ \newline \newline int res = 0; \newline \newline public int \seqsplit{diameterOfBinaryTree(TreeNode} root) \{ \newline traverse(root); \newline return res; \newline \} \newline \newline public int traverse(TreeNode root) \{ \newline if (root == null) \{ \newline return 0; \newline \} \newline int left = traverse(root.left); \newline int right = traverse(root.right); \newline res = Math.max(left + right, res); \newline return Math.max(left, right) + 1; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{2. 两数相加 - 力扣(Leetcode)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{链表格式 \newline \newline class Solution \{ \newline public ListNode addTwoNumbers(ListNode l1, ListNode l2) \{ \newline ListNode first = l1, second = l2; \newline int add = 0; \newline ListNode fakeHead = new ListNode(-1); \newline ListNode res = fakeHead; \newline while (first != null || second != null) \{ \newline int sum = (first == null ? 0 : first.val) + (second == null ? 0 : second.val) + add; \newline add = sum / 10; \newline res.next = new ListNode(sum \% 10); \newline res = res.next; \newline if (first != null) first = first.next; \newline if (second != null) second = second.next; \newline \} \newline if (add != 0) \{ \newline res.next = new ListNode(add \% 10); \newline \} \newline return fakeHead.next; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{138. \seqsplit{复制带随机指针的链表}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline \newline \newline 输入:head = {[}{[}7,null{]},{[}13,0{]},{[}11,4{]},{[}10,2{]},{[}1,0{]}{]} \newline 输出:{[}{[}7,null{]},{[}13,0{]},{[}11,4{]},{[}10,2{]},{[}1,0{]}{]} \newline 示例 2: \newline \newline \newline \newline 输入:head = {[}{[}1,1{]},{[}2,1{]}{]} \newline 输出:{[}{[}1,1{]},{[}2,1{]}{]} \newline 示例 3: \newline \newline \newline \newline 输入:head = {[}{[}3,null{]},{[}3,0{]},{[}3,null{]}{]} \newline 输出:{[}{[}3,null{]},{[}3,0{]},{[}3,null{]}{]} \newline \newline \newline class Solution \{ \newline \newline Map\textless{}Node, Node\textgreater{} map = new HashMap\textless{}Node, Node\textgreater{}(); \newline \newline public Node copyRandomList(Node oldNode) \{ \newline if (oldNode == null) \{ \newline return null; \newline \} \newline if \seqsplit{(map.containsKey(oldNode))} \{ \newline return map.get(oldNode); \newline \} else \{ \newline Node newNode = new Node(oldNode.val); \newline \newline newNode.next = \seqsplit{copyRandomList(oldNode.next);} \newline newNode.random = \seqsplit{copyRandomList(oldNode.random);} \newline map.put(oldNode, newNode); \newline return newNode; \newline \} \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{146. LRU缓存机制}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{public class LRUCache \{ \newline Map\textless{}Integer, Integer\textgreater{} map = new LinkedHashMap\textless{}Integer, Integer\textgreater{}(); \newline int cap; \newline \newline public LRUCache(int capacity) \{ \newline cap = capacity; \newline \} \newline \newline public int get(int key) \{ \newline if (!map.containsKey(key)) \newline return -1; \newline \newline int val = map.remove(key); \newline map.put(key, val); \newline return val; \newline \} \newline \newline public void set(int key, int value) \{ \newline if (map.containsKey(key)) \{ \newline map.remove(key); \newline map.put(key, value); \newline return; \newline \} \newline \newline map.put(key, value); \newline \newline if (map.size() \textgreater{} cap) \newline \seqsplit{map.remove(map.entrySet().iterator().next().getKey());} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{93. 复原IP地址}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline 输入:s = "25525511135" \newline 输出:{[}"255.255.11.135","255.255.111.35"{]} \newline 示例 2: \newline \newline 输入:s = "0000" \newline 输出:{[}"0.0.0.0"{]} \newline 示例 3: \newline \newline 输入:s = "101023" \newline 输出:{[}"1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"{]} \newline \newline \newline \newline class Solution \{ \newline \newline // Temporary list to store parts of the IP address \newline List\textless{}String\textgreater{} temp = new ArrayList\textless{}\textgreater{}(); \newline // Result list to store all valid IP addresses \newline List\textless{}String\textgreater{} res = new ArrayList\textless{}\textgreater{}(); \newline \newline // Public method to start the process of restoring IP addresses \newline public List\textless{}String\textgreater{} \seqsplit{restoreIpAddresses(String} s) \{ \newline // Start backtracking from the first character \newline backtrack(s, 0); \newline // Return the result list \newline return res; \newline \} \newline \newline // Private method to perform backtracking \newline private void backtrack(String s, int curIdx) \{ \newline // If we have 4 parts in the IP address and we haven't reached the end of the string, return \newline if (temp.size() == 4 \&\& curIdx != s.length()) return; \newline // If we have 4 parts in the IP address and we have reached the end of the string, add to result \newline if (temp.size() == 4 \&\& curIdx == s.length())\{ \newline // Join the parts of the IP address with a dot and add to the result list \newline res.add(String.join(".", temp)); \seqsplit{//这个join函数用时2ms,手动StringBuilder的话就是1ms} \newline return; \newline \} \newline // Loop through the next 3 characters of the string \newline for(int i = curIdx; i \textless{} s.length() \&\& i \textless{} curIdx + 3; i++)\{ \newline // Get the substring from the current index to the current index + 1 \newline String sub = s.substring(curIdx, i + 1); \newline // If the integer value of the substring is less than or equal to 255 \newline if (Integer.parseInt(sub) \textless{}= 255)\{ \newline // If the substring has more than one character and the first character is '0', return \newline if(sub.length() \textgreater{} 1 \&\& s.charAt(curIdx) == '0')\{ \newline return; \newline \} \newline // Add the substring to the temporary list \newline temp.add(sub); \newline // Continue backtracking from the next character \newline backtrack(s, i + 1); \newline // Remove the last element from the temporary list \newline temp.remove(temp.size() - 1); \newline \} else \{ \newline // If the integer value of the substring is greater than 255, return \newline return; \newline \} \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{513. 找树左下角的值}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline \newline \newline 输入: root = {[}2,1,3{]} \newline 输出: 1 \newline 示例 2: \newline \newline \newline \newline 输入: {[}1,2,3,4,null,5,6,null,null,7{]} \newline 输出: 7 \newline \newline \newline class Solution \{ \newline public int \seqsplit{findBottomLeftValue(TreeNode} root) \{ \newline Queue\textless{}TreeNode\textgreater{} q = new ArrayDeque\textless{}\textgreater{}(); \newline q.offer(root); \newline int ans = 0; \newline while (!q.isEmpty()) \{ \newline ans = q.peek().val; \newline for (int i = q.size(); i \textgreater{} 0; -{}-i) \{ \newline TreeNode node = q.poll(); \newline if (node.left != null) \{ \newline q.offer(node.left); \newline \} \newline if (node.right != null) \{ \newline q.offer(node.right); \newline \} \newline \} \newline \} \newline return ans; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{129. \seqsplit{求根节点到叶节点数字之和}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给你一个二叉树的根节点} root \seqsplit{,树中每个节点都存放有一个} 0 到 9 之间的数字。 \newline 每条从根节点到叶节点的路径都代表一个数字: \newline \newline 例如,从根节点到叶节点的路径 1 -\textgreater{} 2 -\textgreater{} 3 表示数字 123 。 \newline 计算从根节点到叶节点生成的 所有数字之和 。 \newline \newline 叶节点 \seqsplit{是指没有子节点的节点。} \newline \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}1,2,3{]} \newline 输出:25 \newline 解释: \newline 从根到叶子节点路径 1-\textgreater{}2 代表数字 12 \newline 从根到叶子节点路径 1-\textgreater{}3 代表数字 13 \newline 因此,数字总和 = 12 + 13 = 25 \newline 示例 2: \newline \newline \newline 输入:root = {[}4,9,0,5,1{]} \newline 输出:1026 \newline 解释: \newline 从根到叶子节点路径 4-\textgreater{}9-\textgreater{}5 代表数字 495 \newline 从根到叶子节点路径 4-\textgreater{}9-\textgreater{}1 代表数字 491 \newline 从根到叶子节点路径 4-\textgreater{}0 代表数字 40 \newline 因此,数字总和 = 495 + 491 + 40 = 1026 \newline \newline class Solution \{ \newline public int sumNumbers(TreeNode root) \{ \newline return cal(root, 0); \newline \} \newline \newline public int cal(TreeNode root, int cur) \{ \newline if (root == null) return 0; \newline cur = cur * 10 + root.val; \newline if (root.left == null \&\& root.right == null) return cur; \newline return cal(root.left, cur) + cal(root.right, cur); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{958. \seqsplit{二叉树的完全性检验}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给你一棵二叉树的根节点} root \seqsplit{,请你判断这棵树是否是一棵} 完全二叉树 。 \newline \newline 在一棵 完全二叉树 \seqsplit{中,除了最后一层外,所有层都被完全填满,并且最后一层中的所有节点都尽可能靠左。最后一层(第} h 层)中可以包含 1 到 2h 个节点。 \newline \newline \newline \newline 示例 1: \newline \newline \newline \newline 输入:root = {[}1,2,3,4,5,6{]} \newline 输出:true \newline 解释:最后一层前的每一层都是满的(即,节点值为 \{1\} 和 \{2,3\} 的两层),且最后一层中的所有节点(\{4,5,6\})尽可能靠左。 \newline 示例 2: \newline \newline \newline \newline 输入:root = {[}1,2,3,4,5,null,7{]} \newline 输出:false \newline 解释:值为 7 \seqsplit{的节点不满足条件「节点尽可能靠左」。} \newline \newline \newline // \seqsplit{Java层序遍历,判断size和index是否相等即可} \newline class Solution \{ \newline public boolean isCompleteTree(TreeNode root) \{ \newline if (root == null) return false; \newline LinkedList\textless{}TreeNode\textgreater{} queue = new LinkedList\textless{}\textgreater{}(); \newline LinkedList\textless{}Integer\textgreater{} indexQueue = new LinkedList\textless{}\textgreater{}(); \newline \newline queue.offer(root); \newline indexQueue.offer(0); \newline int maxIndex = 0; \newline int size = -1; \newline while (!queue.isEmpty()) \{ \newline TreeNode node = queue.poll(); \newline int index = indexQueue.poll(); \newline maxIndex = Math.max(index, maxIndex); \newline size++; \newline \newline if (node.left != null) \{ \newline queue.offer(node.left); \newline indexQueue.offer(index {\emph{ 2 + 1); \newline \} \newline if (node.right != null) \{ \newline queue.offer(node.right); \newline indexQueue.offer(index }} 2 + 2); \newline \} \newline \} \newline return size == maxIndex; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{199. 二叉树的右视图}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{输入: {[}1,2,3,null,5,null,4{]} \newline 输出: {[}1,3,4{]} \newline 示例 2: \newline \newline 输入: {[}1,null,3{]} \newline 输出: {[}1,3{]} \newline 示例 3: \newline \newline 输入: {[}{]} \newline 输出: {[}{]} \newline \newline // BFS \newline class Solution \{ \newline public List\textless{}Integer\textgreater{} rightSideView(TreeNode root) \{ \newline List\textless{}Integer\textgreater{} res = new ArrayList\textless{}\textgreater{}(); \newline if (root == null) \{ \newline return res; \newline \} \newline Queue\textless{}TreeNode\textgreater{} queue = new LinkedList\textless{}\textgreater{}(); \newline queue.offer(root); \newline while (!queue.isEmpty()) \{ \newline int size = queue.size(); \newline for (int i = 0; i \textless{} size; i++) \{ \newline TreeNode node = queue.poll(); \newline if (node.left != null) \{ \newline queue.offer(node.left); \newline \} \newline if (node.right != null) \{ \newline queue.offer(node.right); \newline \} \newline if (i == size - 1) \{ \seqsplit{//将当前层的最后一个节点放入结果列表} \newline res.add(node.val); \newline \} \newline \} \newline \} \newline return res; \newline \} \newline \} \newline // DFS \newline 我们按照 「根结点 -\textgreater{} 右子树 -\textgreater{} 左子树」 的顺序访问, \seqsplit{就可以保证每层都是最先访问最右边的节点的。} \newline \newline (与先序遍历 「根结点 -\textgreater{} 左子树 -\textgreater{} 右子树」 \seqsplit{正好相反,先序遍历每层最先访问的是最左边的节点)} \newline class Solution \{ \newline List\textless{}Integer\textgreater{} res = new ArrayList\textless{}\textgreater{}(); \newline \newline public List\textless{}Integer\textgreater{} rightSideView(TreeNode root) \{ \newline dfs(root, 0); // \seqsplit{从根节点开始访问,根节点深度是0} \newline return res; \newline \} \newline \newline private void dfs(TreeNode root, int depth) \{ \newline if (root == null) \{ \newline return; \newline \} \newline // 先访问 \seqsplit{当前节点,再递归地访问} 右子树 和 左子树。 \newline if (depth == res.size()) \{ // \seqsplit{如果当前节点所在深度还没有出现在res里,说明在该深度下当前节点是第一个被访问的节点,因此将当前节点加入res中。} \newline res.add(root.val); \newline \} \newline depth++; \newline dfs(root.right, depth); \newline dfs(root.left, depth); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{206. 反转链表}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline \newline 输入:head = {[}1,2,3,4,5{]} \newline 输出:{[}5,4,3,2,1{]} \newline 示例 2: \newline \newline \newline 输入:head = {[}1,2{]} \newline 输出:{[}2,1{]} \newline 示例 3: \newline \newline 输入:head = {[}{]} \newline 输出:{[}{]} \newline \newline \newline class Solution \{ \newline public ListNode reverseList(ListNode head) \{ \newline ListNode prev = null, next; \newline ListNode curr = head; \newline while (curr != null) \{ \newline next = curr.next; \newline curr.next = prev; \newline prev = curr; \newline curr = next; \newline \} \newline return prev; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{222. \seqsplit{完全二叉树的节点个数}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{给你一棵 完全二叉树 的根节点 root \seqsplit{,求出该树的节点个数。} \newline \newline 完全二叉树 \seqsplit{的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第} h 层,则该层包含 1\textasciitilde{} 2h 个节点。 \newline \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}1,2,3,4,5,6{]} \newline 输出:6 \newline 示例 2: \newline \newline 输入:root = {[}{]} \newline 输出:0 \newline 示例 3: \newline \newline 输入:root = {[}1{]} \newline 输出:1 \newline \newline \newline \newline class Solution \{ \newline public int countNodes(TreeNode root) \{ \newline if (root == null) return 0; \newline TreeNode left = root, right = root; \newline int left\_cnt = 1, right\_cnt = 1; \newline while (left.left != null) \{ \newline left = left.left; \newline left\_cnt++; \newline \} \newline while (right.right != null) \{ \newline right = right.right; \newline right\_cnt++; \newline \} \newline if (left\_cnt == right\_cnt) \{ \newline return (int)Math.pow(2, left\_cnt) - 1; \newline \} \newline return 1 + countNodes(root.left) + countNodes(root.right); \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{235. \seqsplit{二叉搜索树的最近公共祖先}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给定一个二叉搜索树}, \seqsplit{找到该树中两个指定节点的最近公共祖先。} \newline \newline 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 \seqsplit{p、q,最近公共祖先表示为一个结点} x,满足 x 是 p、q 的祖先且 x \seqsplit{的深度尽可能大(一个节点也可以是它自己的祖先)。"} \newline \newline 例如,给定如下二叉搜索树: root = {[}6,2,8,0,4,7,9,null,null,3,5{]} \newline \newline \newline \newline 示例 1: \newline \newline 输入: root = {[}6,2,8,0,4,7,9,null,null,3,5{]}, p = 2, q = 8 \newline 输出: 6 \newline 解释: 节点 2 和节点 8 的最近公共祖先是 6。 \newline 示例 2: \newline \newline 输入: root = {[}6,2,8,0,4,7,9,null,null,3,5{]}, p = 2, q = 4 \newline 输出: 2 \newline 解释: 节点 2 和节点 4 的最近公共祖先是 2, \seqsplit{因为根据定义最近公共祖先节点可以为节点本身。} \newline \newline \newline \newline \newline class Solution \{ \newline public TreeNode \seqsplit{lowestCommonAncestor(TreeNode} root, TreeNode p, TreeNode q) \{ \newline if (p.val \textgreater{} q.val) \{ \newline return traverse(root, q, p); \newline \} \newline return traverse(root, p, q); \newline \} \newline \newline public TreeNode traverse(TreeNode root, TreeNode p, TreeNode q) \{ \newline if (root == null) return null; \newline if (root == p || root == q) return root; \newline if (root.val \textless{} p.val) \{ \newline return traverse(root.right, p, q); \newline \} else if (root.val \textgreater{} q.val) \{ \newline return traverse(root.left, p, q); \newline \} \newline return root; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{341. \seqsplit{扁平化嵌套列表迭代器}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline 输入:nestedList = {[}{[}1,1{]},2,{[}1,1{]}{]} \newline 输出:{[}1,1,2,1,1{]} \newline 解释:通过重复调用 next 直到 hasNext 返回 false,next \seqsplit{返回的元素的顺序应该是:} {[}1,1,2,1,1{]}。 \newline 示例 2: \newline \newline 输入:nestedList = {[}1,{[}4,{[}6{]}{]}{]} \newline 输出:{[}1,4,6{]} \newline 解释:通过重复调用 next 直到 hasNext 返回 false,next \seqsplit{返回的元素的顺序应该是:} {[}1,4,6{]}。 \newline \newline \newline \newline public class NestedIterator implements Iterator\textless{}Integer\textgreater{} \{ \newline \newline LinkedList\textless{}NestedInteger\textgreater{} list; \newline \newline public NestedIterator(List\textless{}NestedInteger\textgreater{} nestedList) \{ \newline list = new LinkedList\textless{}\textgreater{}(nestedList); \newline \} \newline \newline @Override \newline public Integer next() \{ \newline return \seqsplit{list.removeFirst().getInteger();} \newline \} \newline \newline @Override \newline public boolean hasNext() \{ \newline while (!list.isEmpty() \&\& !list.getFirst().isInteger()) \{ \newline List\textless{}NestedInteger\textgreater{} cur\_list = \seqsplit{list.removeFirst().getList();} \newline for (int i = cur\_list.size() - 1; i \textgreater{}= 0; i-{}-) \{ \newline \seqsplit{list.addFirst(cur\_list.get(i));} \newline \} \newline \} \newline return !list.isEmpty(); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{96. 不同的二叉搜索树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{给你一个整数 n ,求恰由 n \seqsplit{个节点组成且节点值从} 1 到 n 互不相同的 二叉搜索树 \seqsplit{有多少种?返回满足题意的二叉搜索树的种数。} \newline \newline \newline 示例 1: \newline 输入:n = 3 \newline 输出:5 \newline 示例 2: \newline 输入:n = 1 \newline 输出:1 \newline \newline \newline class Solution \{ \newline \newline public int numTrees(int n) \{ \newline return sum(1,n); \newline \} \newline \newline private int sum(int left,int right)\{ \newline if(right\textless{}=left) return 1; \newline int sum = 0; \newline for(int i=left;i\textless{}=right;i++) sum += sum(left,i-1) * sum(i+1,right); \newline return sum; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{98. 验证二叉搜索树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给你一个二叉树的根节点} root \seqsplit{,判断其是否是一个有效的二叉搜索树。} \newline \newline 有效 \seqsplit{二叉搜索树定义如下:} \newline \newline 节点的左子树只包含 小于 当前节点的数。 \newline 节点的右子树只包含 大于 当前节点的数。 \newline 所有左子树和右子树自身必须也是二叉搜索树。 \newline \newline \newline 示例 1: \newline \newline \newline 输入:root = {[}2,1,3{]} \newline 输出:true \newline 示例 2: \newline \newline \newline 输入:root = {[}5,1,4,null,null,3,6{]} \newline 输出:false \newline 解释:根节点的值是 5 \seqsplit{,但是右子节点的值是} 4 。 \newline \newline \newline class Solution \{ \newline TreeNode pre = null; \newline public boolean isValidBST(TreeNode root) \{ \newline if (root == null) \{ \newline return true; \newline \} \newline if (! isValidBST(root.left)) \{ \newline return false; \newline \} \newline if (pre != null \&\& root.val \textless{}= pre.val) \{ \newline return false; \newline \} \newline pre = root; \newline return isValidBST(root.right); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{652. 寻找重复的子树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{输入:root = {[}1,2,3,4,null,2,4,null,null,4{]} \newline 输出:{[}{[}2,4{]},{[}4{]}{]} \newline 示例 2: \newline \newline 输入:root = {[}2,1,1{]} \newline 输出:{[}{[}1{]}{]} \newline 示例 3: \newline \newline 输入:root = {[}2,2,2,3,null,3,null{]} \newline 输出:{[}{[}2,3{]},{[}3{]}{]} \newline \newline class Solution \{ \newline \newline HashMap\textless{}String, Integer\textgreater{} map = new HashMap\textless{}\textgreater{}(); \newline List\textless{}TreeNode\textgreater{} res = new LinkedList\textless{}\textgreater{}(); \newline \newline public List\textless{}TreeNode\textgreater{} \seqsplit{findDuplicateSubtrees(TreeNode} root) \{ \newline traverse(root); \newline return res; \newline \} \newline \newline public String traverse(TreeNode root) \{ \newline if (root == null) return "null"; \newline String left = traverse(root.left); \newline String right = traverse(root.right); \newline String str = root.val + "," + left + "," + right; \newline if (map.containsKey(str) \&\& map.get(str) == 1) \{ \newline res.add(root); \newline \} \newline map.put(str, map.getOrDefault(str, 0) + 1); \newline return str; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{654. 最大二叉树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\seqsplit{给定一个不重复的整数数组} nums 。 最大二叉树 \seqsplit{可以用下面的算法从} nums 递归地构建: \newline \newline 创建一个根节点,其值为 nums 中的最大值。 \newline 递归地在最大值 左边 的 子数组前缀上 构建左子树。 \newline 递归地在最大值 右边 的 子数组后缀上 构建右子树。 \newline 返回 nums 构建的 最大二叉树 。 \newline \newline 输入:nums = {[}3,2,1,6,0,5{]} \newline 输出:{[}6,3,5,null,2,0,null,null,1{]} \newline 解释:递归调用如下所示: \newline - {[}3,2,1,6,0,5{]} 中的最大值是 6 ,左边部分是 {[}3,2,1{]} ,右边部分是 {[}0,5{]} 。 \newline - {[}3,2,1{]} 中的最大值是 3 ,左边部分是 {[}{]} ,右边部分是 {[}2,1{]} 。 \newline - \seqsplit{空数组,无子节点。} \newline - {[}2,1{]} 中的最大值是 2 ,左边部分是 {[}{]} ,右边部分是 {[}1{]} 。 \newline - \seqsplit{空数组,无子节点。} \newline - \seqsplit{只有一个元素,所以子节点是一个值为} 1 的节点。 \newline - {[}0,5{]} 中的最大值是 5 ,左边部分是 {[}0{]} ,右边部分是 {[}{]} 。 \newline - \seqsplit{只有一个元素,所以子节点是一个值为} 0 的节点。 \newline - \seqsplit{空数组,无子节点。} \newline \newline \newline class Solution \{ \newline public TreeNode constructMaximumBinaryTree(int{[}{]} nums) \{ \newline return maxTree(nums, 0, nums.length - 1); \newline \} \newline \newline public TreeNode maxTree(int{[}{]} nums, int l, int r)\{ \newline if(l \textgreater{} r)\{ \newline return null; \newline \} \newline \seqsplit{//bond为当前数组中最大值的索引} \newline int idx = findMax(nums, l, r); \newline TreeNode root = new TreeNode(nums{[}bond{]}); \newline root.left = maxTree(nums, l, idx - 1); \newline root.right = maxTree(nums, idx + 1, r); \newline return root; \newline \} \newline //找最大值的索引 \newline public int findMax(int{[}{]} nums, int l, int r)\{ \newline int max = Integer.MIN\_VALUE, maxIndex = l; \newline for(int i = l; i \textless{}= r; i++)\{ \newline if(max \textless{} nums{[}i{]})\{ \newline max = nums{[}i{]}; \newline maxIndex = i; \newline \} \newline \} \newline return maxIndex; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{116. \seqsplit{填充每个节点的下一个右指针}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline public Node connect(Node root) \{ \newline if (root == null) return null; \newline traverse(root.left, root.right); \newline return root; \newline \} \newline \newline public void traverse(Node left, Node right) \{ \newline if (left == null || right == null) return; \newline left.next = right; \newline traverse(left.left, left.right); \newline traverse(right.left, right.right); \newline traverse(left.right, right.left); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{226. 翻转二叉树}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline public TreeNode invertTree(TreeNode root) \{ \newline if (root == null) return null; \newline TreeNode left = invertTree(root.left); \newline TreeNode right = invertTree(root.right); \newline root.right = left; \newline root.left = right; \newline return root; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{515. \seqsplit{在每个树行中找最大值}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例1: \newline \newline \newline \newline 输入: root = {[}1,3,2,5,3,null,9{]} \newline 输出: {[}1,3,9{]} \newline 示例2: \newline \newline 输入: root = {[}1,2,3{]} \newline 输出: {[}1,3{]} \newline \newline \newline // dfs(root, 结果, 层级) \newline // 如果root是null返回 \newline // \newline class Solution \{ \newline // \seqsplit{二叉树层序遍历寻找最大值} \newline public List\textless{}Integer\textgreater{} largestValues(TreeNode root) \{ \newline List\textless{}Integer\textgreater{} res = new LinkedList\textless{}\textgreater{}(); \newline if (null == root) \{ \newline return res; \newline \} \newline LinkedList\textless{}TreeNode\textgreater{} queue = new LinkedList\textless{}\textgreater{}(); \newline queue.add(root); \newline while (!queue.isEmpty()) \{ \newline int size = queue.size(); \newline int max = queue.getFirst().val; \newline for (int i = 0; i \textless{} size; i++) \{ \newline TreeNode node = queue.removeFirst(); \newline if (node.val \textgreater{} max) \{ \newline max = node.val; \newline \} \newline if (node.left != null) \{ \newline \seqsplit{queue.addLast(node.left);} \newline \} \newline if (node.right != null) \{ \newline \seqsplit{queue.addLast(node.right);} \newline \} \newline \} \newline res.add(max); \newline \} \newline return res; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{230. \seqsplit{二叉搜索树中第K小的元素}}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{class Solution \{ \newline \newline int k = 0; \newline int res = 0; \newline \newline public int kthSmallest(TreeNode root, int k) \{ \newline this.k = k; \newline traverse(root); \newline return res; \newline \} \newline \newline public void traverse(TreeNode root) \{ \newline if (root == null) \{ \newline return; \newline \} \newline traverse(root.left); \newline k-{}-; \newline if (k == 0) \{ \newline res = root.val; \newline return; \newline \} \newline traverse(root.right); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{78. 子集}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline 输入:nums = {[}1,2,3{]} \newline 输出:{[}{[}{]},{[}1{]},{[}2{]},{[}1,2{]},{[}3{]},{[}1,3{]},{[}2,3{]},{[}1,2,3{]}{]} \newline 示例 2: \newline \newline 输入:nums = {[}0{]} \newline 输出:{[}{[}{]},{[}0{]}{]} \newline \newline \newline \newline class Solution \{ \newline \seqsplit{//定义二维数组res用于存储结果} \newline List\textless{}List\textless{}Integer\textgreater{}\textgreater{} res = new LinkedList\textless{}\textgreater{}(); \newline \newline public List\textless{}List\textless{}Integer\textgreater{}\textgreater{} subsets(int{[}{]} nums) \{ \newline //定义路径数组 \newline List\textless{}Integer\textgreater{} track = new LinkedList\textless{}\textgreater{}(); \newline backtrack(nums, 0, track); \newline \newline return res; \newline \} \newline \newline public void backtrack(int{[}{]} nums, int start, List\textless{}Integer\textgreater{} track) \{ \newline \seqsplit{//添加路径数组到结果数组中} \newline res.add(new LinkedList\textless{}\textgreater{}(track)); \newline \seqsplit{//for循环遍历数组nums} \newline for (int i = start; i \textless{} nums.length; i++) \{ \newline \seqsplit{//做选择,将选择添加到路径数组中} \newline track.add(nums{[}i{]}); \newline \seqsplit{//回溯,继续向后遍历} \newline backtrack(nums, i + 1, track); \newline \seqsplit{//撤销选择,将选择从路径中删除} \newline \seqsplit{track.remove(track.size()} - 1); \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{125. 验证回文串}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{示例 1: \newline \newline 输入: s = "A man, a plan, a canal: Panama" \newline 输出:true \newline 解释:"amanaplanacanalpanama" 是回文串。 \newline 示例 2: \newline \newline 输入:s = "race a car" \newline 输出:false \newline 解释:"raceacar" 不是回文串。 \newline 示例 3: \newline \newline 输入:s = " " \newline 输出:true \newline 解释:在移除非字母数字字符之后,s 是一个空字符串 "" 。 \newline 由于空字符串正着反着读都一样,所以是回文串。 \newline \newline \newline class Solution \{ \newline public boolean isPalindrome(String s) \{ \newline int n = s.length(); \newline int left = 0, right = n - 1; \newline while (left \textless{} right) \{ \newline while (left \textless{} right \&\& !Character.isLetterOrDigit(s.charAt(left))) \{ \newline left++; \newline \} \newline while (left \textless{} right \&\& !Character.isLetterOrDigit(s.charAt(right))) \{ \newline right-{}-; \newline \} \newline if (left \textless{} right) \{ \newline if \seqsplit{(Character.toLowerCase(s.charAt(left))} != \seqsplit{Character.toLowerCase(s.charAt(right)))} \{ \newline return false; \newline \} \newline left++; \newline right-{}-; \newline \} \newline \} \newline return true; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}