<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-CN"><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://wind9.xyz/feed.xml" rel="self" type="application/atom+xml" /><link href="https://wind9.xyz/" rel="alternate" type="text/html" hreflang="zh-CN" /><updated>2026-10-29T11:38:55+08:00</updated><id>https://wind9.xyz/feed.xml</id><title type="html">wind989 的技术博客</title><subtitle>记录 Python 后端与 AI Agent 学习</subtitle><author><name>wind989</name></author><entry><title type="html">从翻转到对称：一次二叉树刷题复盘</title><link href="https://wind9.xyz/leetcode/invert-and-symmetric-tree-notes/" rel="alternate" type="text/html" title="从翻转到对称：一次二叉树刷题复盘" /><published>2026-10-29T00:00:00+08:00</published><updated>2026-10-29T00:00:00+08:00</updated><id>https://wind9.xyz/leetcode/invert-and-symmetric-tree-notes</id><content type="html" xml:base="https://wind9.xyz/leetcode/invert-and-symmetric-tree-notes/">&lt;p&gt;今天上午继续刷二叉树，学习了第 226 题翻转二叉树，又接着做第 101 题对称二叉树。两道题看起来都和“翻转”有关，连着做的时候，我确实想把前一道题的方法直接用到后一道题上。&lt;/p&gt;

&lt;p&gt;不过，这次更值得记下来的，是几个很基础的问题：为什么能直接返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;？给树换一个变量名，算不算保存了原来的树？递归比较两个节点时，应该先判断什么？&lt;/p&gt;

&lt;h2 id=&quot;为什么返回-root就能拿到整棵树&quot;&gt;为什么返回 root，就能拿到整棵树&lt;/h2&gt;

&lt;p&gt;看翻转二叉树时，我问了一个问题：为什么可以直接返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;，而不是把节点值存进列表，再返回这个列表？&lt;/p&gt;

&lt;p&gt;我当时容易把题目里展示的数组，当成程序里实际操作的数据。实际上，题目中的数组是树的一种表示方式；在这道题的函数里，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; 是根节点对象的引用，节点通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right&lt;/code&gt; 连接到它的左右孩子。&lt;/p&gt;

&lt;p&gt;所以，返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt; 不是只返回根节点的那个数字。调用方拿到这个节点后，可以沿着左右孩子的引用，继续访问整棵树。这里也没有自动把所有节点打包成一个新对象，更没有复制一棵树。&lt;/p&gt;

&lt;p&gt;可以把它理解成：&lt;strong&gt;根节点是访问整棵树的入口。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;如果左孩子存在，下面两行就能说明这种关系：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这时 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root.left.val&lt;/code&gt; 也会是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;100&lt;/code&gt;。因为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root.left&lt;/code&gt; 指向同一个节点，修改的是同一个对象。&lt;/p&gt;

&lt;p&gt;这也解释了后面那个更容易踩的坑：赋值通常只是让名字绑定到对象，并不会复制对象。&lt;a href=&quot;https://docs.python.org/3/tutorial/classes.html#a-word-about-names-and-objects&quot;&gt;Python 官方文档对名字与对象的说明&lt;/a&gt;也专门讲到了这种共享引用的情况。&lt;/p&gt;

&lt;h2 id=&quot;翻转二叉树改的是节点之间的连接&quot;&gt;翻转二叉树，改的是节点之间的连接&lt;/h2&gt;

&lt;p&gt;翻转一棵树，可以在每个节点处交换它的左右孩子，再递归处理两边的子树。这样交换的是整棵子树的入口，不只是左右孩子的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;整理后的写法如下，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TreeNode&lt;/code&gt; 使用力扣提供的定义：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;invertTree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;old_left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old_left&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;old_left&lt;/code&gt; 暂时保存原来左孩子的引用，避免赋值时把它丢掉；它同样没有复制节点。&lt;/p&gt;

&lt;p&gt;这里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;digui()&lt;/code&gt; 和之前算&lt;a href=&quot;/leetcode/104-maximum-depth-of-binary-tree/&quot;&gt;最大深度&lt;/a&gt;的递归不太一样。算深度时，需要接收左右子树返回的数字；这次的辅助函数直接修改节点的属性，不必再返回一个计算结果。最外层的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invertTree()&lt;/code&gt; 则按题目要求，返回修改后的树的根节点。&lt;/p&gt;

&lt;p&gt;也就是说，递归函数做了什么，和它返回什么，是两件需要分别想清楚的事。&lt;/p&gt;

&lt;h2 id=&quot;我想用翻转后比较判断对称却忽略了引用&quot;&gt;我想用“翻转后比较”判断对称，却忽略了引用&lt;/h2&gt;

&lt;p&gt;接着做对称二叉树时，我的想法是：如果一棵树翻转后和原来一样，那它不就是对称的吗？&lt;/p&gt;

&lt;p&gt;这个判断方向有道理。但我的代码里，用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = root&lt;/code&gt; 来保存“原来的树”，再原地翻转 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;，最后比较 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x == root&lt;/code&gt;。当时的关键片段是：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 当时的思路片段，不能用来判断树是否对称
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# 原地翻转
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;问题就在第一行。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = root&lt;/code&gt; 只是让两个名字指向同一个根节点。翻转时修改了原树，通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; 看到的也已经是修改后的树，它并不是翻转前的快照。&lt;/p&gt;

&lt;p&gt;因此，这段代码始终在拿同一个对象和自己比较，无法判断树是否对称。&lt;/p&gt;

&lt;p&gt;这里还有一个知识点：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is&lt;/code&gt; 判断是不是同一个对象；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt; 的含义取决于类型怎样定义相等。力扣常见的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TreeNode&lt;/code&gt; 定义没有实现按整棵树的结构和值进行比较的逻辑，不能把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x == root&lt;/code&gt; 当成“两棵树逐个节点比较”。即使补上结构比较，也不能解决两个变量指向同一棵已被修改的树的问题。&lt;/p&gt;

&lt;p&gt;这个坑提醒我，在用一个变量“保存原值”之前，得先确认保存的是独立的数据，还是同一个对象的另一个名字。&lt;/p&gt;

&lt;h2 id=&quot;判断对称可以直接比较镜像位置&quot;&gt;判断对称，可以直接比较镜像位置&lt;/h2&gt;

&lt;p&gt;与其真的翻转树，不如直接比较左右两边是否互为镜像。&lt;/p&gt;

&lt;p&gt;这次递归要同时接收两个节点。它的任务可以先用一句话说明：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mirror(left, right)&lt;/code&gt; 判断以这两个节点为根的子树，是否互为镜像。&lt;/p&gt;

&lt;p&gt;两个节点对应时，不仅值要相同，位置也要相互对照：左边的左孩子，对应右边的右孩子；左边的右孩子，对应右边的左孩子。&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;          根节点
         /      \
      left      right

外侧：left.left   ↔ right.right
内侧：left.right  ↔ right.left
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;代码可以这样写：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isSymmetric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mirror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;mirror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mirror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mirror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;两边都空，说明这个位置对应；只有一边为空，结构就不对称；两边都存在，才有必要比较节点值，再递归比较外侧和内侧。这里用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt;，因为两组位置必须都对应，整体才是镜像。&lt;/p&gt;

&lt;p&gt;尤其要注意，节点值相同还不够。假如根的左右孩子都是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;，而它们各自都只有一个右孩子 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;，两边虽然长得一样，却不是镜像：一边的右孩子应该对应另一边的左孩子。&lt;/p&gt;

&lt;h2 id=&quot;一次语法报错还带出了判空顺序的问题&quot;&gt;一次语法报错，还带出了判空顺序的问题&lt;/h2&gt;

&lt;p&gt;这次提交里，我把辅助函数的定义写成了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;def digui(root.left, root.right):&lt;/code&gt;，结果提示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SyntaxError&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;原因是把“定义函数”和“调用函数”的参数写法混在一起了。定义时要给形参起名字，调用时才把实际节点传进去：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 定义：left、right 是接收节点的变量名
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 调用：把具体的左右孩子传进去
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;回头看同一段代码，判断顺序也有问题：我先访问了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left.val&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;right.val&lt;/code&gt;，后面才判断节点是不是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt;。如果某一边为空，程序在读取属性时就会出错，根本走不到后面的判空。&lt;/p&gt;

&lt;p&gt;另外，我还把“有一边为空就返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;”放在了“两边都空就返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;”之前。两边都空时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left is None or right is None&lt;/code&gt; 同样成立，于是提前返回了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;把各种情况列出来，顺序就清楚了：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;当前两个位置&lt;/th&gt;
      &lt;th&gt;结果或下一步&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;两边都是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;只有一边是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;两边存在，但值不同&lt;/td&gt;
      &lt;td&gt;返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;两边存在，值也相同&lt;/td&gt;
      &lt;td&gt;继续比较外侧和内侧&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;前面的分支返回以后，后面的代码才能依赖已经确认的条件。例如，走到比较 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.val&lt;/code&gt; 那一行时，两个节点都一定存在。&lt;/p&gt;

&lt;h2 id=&quot;这次留下的经验&quot;&gt;这次留下的经验&lt;/h2&gt;

&lt;p&gt;前两天做&lt;a href=&quot;/leetcode/94-binary-tree-inorder-traversal/&quot;&gt;中序遍历&lt;/a&gt;和最大深度时，我开始注意递归的访问顺序和返回值。今天这两道题，又让我把“对象有没有被修改”加了进来。&lt;/p&gt;

&lt;p&gt;放在一起看，几种递归的职责其实不同：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;问题&lt;/th&gt;
      &lt;th&gt;递归主要在做什么&lt;/th&gt;
      &lt;th&gt;最外层返回什么&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;中序遍历&lt;/td&gt;
      &lt;td&gt;按顺序把节点值加入列表&lt;/td&gt;
      &lt;td&gt;结果列表&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;最大深度&lt;/td&gt;
      &lt;td&gt;合并左右子树返回的深度&lt;/td&gt;
      &lt;td&gt;数字&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;翻转二叉树&lt;/td&gt;
      &lt;td&gt;修改每个节点的左右孩子引用&lt;/td&gt;
      &lt;td&gt;根节点引用&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;对称二叉树&lt;/td&gt;
      &lt;td&gt;比较两棵子树是否互为镜像&lt;/td&gt;
      &lt;td&gt;布尔值&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;翻转和镜像比较都最多访问每个节点一次，时间复杂度为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;，递归栈空间为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(h)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; 是树高。树很深时，还要考虑 Python 的递归层数限制，不能只看代码是否简短。&lt;/p&gt;

&lt;p&gt;对我来说，这次最有用的教训有三个：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;写递归前，先说清楚函数接收什么、返回什么，以及是否会修改传入的对象。&lt;/li&gt;
  &lt;li&gt;把前一道题的方法用到新题时，重新检查题目要求，尤其是对象引用和数据有没有被改变。&lt;/li&gt;
  &lt;li&gt;先处理空节点等边界情况，再访问属性；分支之间有重叠时，想一想谁应该先判断。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这次从“为什么能返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;”问起，最后连到了对象引用、函数参数和递归边界。看起来是几个小问题，真正写代码时却会连在一起。把这些地方弄清楚，比只记住翻转和对称的两段代码更有收获。&lt;/p&gt;

&lt;p&gt;题目链接：&lt;a href=&quot;https://leetcode.cn/problems/invert-binary-tree/&quot;&gt;力扣 226 · 翻转二叉树&lt;/a&gt;、&lt;a href=&quot;https://leetcode.cn/problems/symmetric-tree/&quot;&gt;力扣 101 · 对称二叉树&lt;/a&gt;。&lt;/p&gt;</content><author><name>wind989</name></author><category term="力扣刷题" /><summary type="html">为什么返回 root 就能返回整棵树？x = root 为什么没有保存原树？今天上午的两道二叉树题，把对象引用、递归返回值和判空顺序这些基础问题连在了一起。</summary></entry><entry><title type="html">力扣 94：二叉树的中序遍历，把递归的顺序想清楚</title><link href="https://wind9.xyz/leetcode/94-binary-tree-inorder-traversal/" rel="alternate" type="text/html" title="力扣 94：二叉树的中序遍历，把递归的顺序想清楚" /><published>2026-10-27T00:00:00+08:00</published><updated>2026-10-27T00:00:00+08:00</updated><id>https://wind9.xyz/leetcode/binary-tree-inorder-traversal</id><content type="html" xml:base="https://wind9.xyz/leetcode/94-binary-tree-inorder-traversal/">&lt;p&gt;今天记录两道二叉树的题，先从中序遍历开始。我想把“左、根、右”这句话和代码真正对应起来：递归走到了哪里，什么时候记录节点，又是什么时候回到上一层。&lt;/p&gt;

&lt;h2 id=&quot;中序指的是根节点放在中间&quot;&gt;中序，指的是根节点放在中间&lt;/h2&gt;

&lt;p&gt;这道题要把树里的节点按中序访问，返回它们的值。具体顺序是：先访问左子树，再记录当前节点，最后访问右子树。&lt;/p&gt;

&lt;p&gt;这里的“左子树”不只是当前节点的左孩子，而是以左孩子为根的整棵树。走进这棵子树以后，还是按同样的顺序处理。&lt;/p&gt;

&lt;p&gt;拿下面这棵树举例：&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       4
      / \
     2   6
    / \
   1   3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在记录根节点 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt; 之前，需要先处理完整个左子树，所以顺序是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1、2、3&lt;/code&gt;，接着才是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4&lt;/code&gt;，最后是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6&lt;/code&gt;。结果为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[1, 2, 3, 4, 6]&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;这棵树恰好是一棵二叉搜索树，所以结果有序。普通二叉树的中序结果并不一定有序，顺序仍然是左、根、右。&lt;/p&gt;

&lt;h2 id=&quot;递归代码为什么这样写&quot;&gt;递归代码为什么这样写&lt;/h2&gt;

&lt;p&gt;下面的代码使用力扣提供的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TreeNode&lt;/code&gt; 节点定义：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inorderTraversal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dfs(node)&lt;/code&gt; 在这里的任务，是按中序把当前这棵子树的节点值加入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result&lt;/code&gt;。遇到空节点，就没有东西可以记录，直接结束这次调用。&lt;/p&gt;

&lt;p&gt;关键是中间这三行：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;# 先处理完左子树
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;# 再记录当前节点
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;# 最后处理右子树
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;第一行没有执行完，第二行就不会执行。递归进入左子树后，当前这一层会停在这里等待；等左子树处理结束，才继续记录当前节点。&lt;/p&gt;

&lt;p&gt;以节点 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; 为例，先进入节点 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; 的左边为空，返回后记录 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;；右边也为空，返回到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;，这时才记录 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;，然后进入节点 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;。这样就得到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1、2、3&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;如果把记录节点的语句放到两次递归之前，就是根、左、右的前序遍历；放到两次递归之后，就是左、右、根的后序遍历。&lt;strong&gt;判断遍历顺序，可以看“记录当前节点”发生在什么时候。&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;不用递归时用栈记住回来的路&quot;&gt;不用递归时，用栈记住回来的路&lt;/h2&gt;

&lt;p&gt;递归能回到上一层，是因为调用栈保存了还没有做完的事情。手动使用一个栈，也可以保留这条回来的路：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inorderTraversal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;先一路往左走，把沿途节点放进栈。走到空位置后，弹出的节点就是接下来该记录的节点；记录后转向它的右子树，再重复这个过程。&lt;/p&gt;

&lt;p&gt;外层循环不能只判断 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;current&lt;/code&gt;。即使它为空，栈里也可能还有等待处理的祖先节点。&lt;/p&gt;

&lt;p&gt;两种写法都访问每个节点一次，时间复杂度是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;。除了存放答案的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt; 空间，递归调用栈或手动维护的栈最多占 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(h)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; 是树高；树退化成一条链时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; 可以等于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;。&lt;/p&gt;

&lt;h2 id=&quot;这道题留下的方法&quot;&gt;这道题留下的方法&lt;/h2&gt;

&lt;p&gt;理解递归时，我会先说明当前函数负责什么，再检查空节点怎么处理，最后看当前节点的操作放在哪里。把一小棵树的执行过程走一遍，比只记住几行代码更容易把顺序想清楚。&lt;/p&gt;

&lt;p&gt;接着看&lt;a href=&quot;/leetcode/104-maximum-depth-of-binary-tree/&quot;&gt;二叉树的最大深度&lt;/a&gt;时，还有一个值得对照的地方：中序遍历可以把结果写进同一个列表，而最大深度需要把子树的计算结果一层层返回。&lt;/p&gt;

&lt;p&gt;题目链接：&lt;a href=&quot;https://leetcode.cn/problems/binary-tree-inorder-traversal/&quot;&gt;力扣 94 · 二叉树的中序遍历&lt;/a&gt;。&lt;/p&gt;</content><author><name>wind989</name></author><category term="力扣刷题" /><summary type="html">中序遍历的顺序是左子树、根节点、右子树。把这句话放到每一棵子树上，再看递归什么时候返回，代码就容易理解了。</summary></entry><entry><title type="html">力扣 104：二叉树的最大深度，递归的数字是怎么返回来的</title><link href="https://wind9.xyz/leetcode/104-maximum-depth-of-binary-tree/" rel="alternate" type="text/html" title="力扣 104：二叉树的最大深度，递归的数字是怎么返回来的" /><published>2026-10-27T00:00:00+08:00</published><updated>2026-10-27T00:00:00+08:00</updated><id>https://wind9.xyz/leetcode/maximum-depth-of-binary-tree</id><content type="html" xml:base="https://wind9.xyz/leetcode/104-maximum-depth-of-binary-tree/">&lt;p&gt;做完中序遍历后，我接着看了二叉树的最大深度。这次最让我想弄清楚的问题是：为什么 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dfs(root)&lt;/code&gt; 就能返回当前这棵树的最大深度？这个数字到底是怎么算出来的？&lt;/p&gt;

&lt;h2 id=&quot;我最开始的想法问题出在哪里&quot;&gt;我最开始的想法，问题出在哪里&lt;/h2&gt;

&lt;p&gt;我一开始的写法，大致是先递归左子树，再让 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; 加一；递归右子树，再让 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt; 加一，最后比较两边的大小。关键部分像这样：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 最初的思路片段，这样写并不能计算子树深度
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digui&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;问题是，每次进入函数，都会重新创建这一层自己的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt;，并把它们设为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;。执行完递归再加一，只会让当前这一层的变量从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; 变成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;，并没有得到子树到底有多深。&lt;/p&gt;

&lt;p&gt;而且，调用了递归函数，不代表它的计算结果会自动放进当前变量里。需要让子树返回深度，再用变量接收这个返回值。&lt;/p&gt;

&lt;p&gt;所以，这道题首先要讲清楚：&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dfs(node)&lt;/code&gt; 返回以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node&lt;/code&gt; 为根的这棵子树的最大深度。&lt;/strong&gt;这句话是我们给函数规定的任务，后面的代码要把这个任务实现出来。&lt;/p&gt;

&lt;h2 id=&quot;先从最小的树开始算&quot;&gt;先从最小的树开始算&lt;/h2&gt;

&lt;p&gt;这道题的深度按路径上的节点数计算。空树深度是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;，只有一个节点的树深度是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;如果当前节点存在，它的左右子树分别算出了深度，那么从当前节点出发，当然要选择更深的那一边，再加上当前节点这一层：&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;当前子树的深度 = max(左子树深度, 右子树深度) + 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+1&lt;/code&gt; 是当前节点这一层，不是给每一次递归调用都随意记一次数。&lt;/p&gt;

&lt;p&gt;看下面这棵树：&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       5
      / \
     2   8
        /
       7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;当前节点&lt;/th&gt;
      &lt;th&gt;左子树返回&lt;/th&gt;
      &lt;th&gt;右子树返回&lt;/th&gt;
      &lt;th&gt;当前节点返回&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;虽然调用从根节点向下展开，深度的计算却要等子树返回后才能完成。叶子节点先返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;，父节点拿到这个数字算出自己的深度，再继续向上返回，最终根节点得到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3&lt;/code&gt;。&lt;/p&gt;

&lt;h2 id=&quot;把返回值接住代码就连起来了&quot;&gt;把返回值接住，代码就连起来了&lt;/h2&gt;

&lt;p&gt;下面的代码使用力扣提供的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TreeNode&lt;/code&gt; 节点定义：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;maxDepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;left_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;right_depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;right_depth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dfs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_depth = dfs(node.left)&lt;/code&gt; 的意思是，先把左子树的深度算完，然后把返回的数字交给 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;left_depth&lt;/code&gt;；右边同理。两边都拿到结果后，当前这一层才能完成自己的计算。&lt;/p&gt;

&lt;p&gt;原来分别用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;else&lt;/code&gt; 比较大小，再返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l + 1&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r + 1&lt;/code&gt;，也完全可以。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;max()&lt;/code&gt; 只是把这一步写得更紧凑。&lt;/p&gt;

&lt;p&gt;最外层也要 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return dfs(root)&lt;/code&gt;。否则即使里面算出了数字，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxDepth()&lt;/code&gt; 仍然没有把答案交出去。&lt;/p&gt;

&lt;p&gt;我最后把思路整理成了这段话：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;用递归处理一棵树，节点为空就返回 0。分别递归左子树和右子树，用变量接收它们返回的深度，取较大值再加 1，得到当前这棵子树的深度。这样一层层返回，最终就能得到整棵树的最大深度。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;递归不是调用一次函数就自动知道答案。空树提供了最小的已知结果，每一层都按照同一条规则，把更小的问题的答案组合起来。&lt;/p&gt;

&lt;h2 id=&quot;树很深时也可以按层数&quot;&gt;树很深时，也可以按层数&lt;/h2&gt;

&lt;p&gt;递归写法的时间复杂度是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;，调用栈空间是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(h)&lt;/code&gt;。如果树退化成很长的一条链，Python 的递归层数限制可能导致 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RecursionError&lt;/code&gt;。这时可以用队列做层序遍历：每处理完一整层，深度加一。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deque&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Solution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;maxDepth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TreeNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deque&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;level_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;level_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;popleft&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depth&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;每轮开始先记住当前层的节点数，只处理这批节点；新加入的孩子留到下一轮。这样 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;depth&lt;/code&gt; 增加一次，就对应完整地走过一层。时间复杂度仍是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(n)&lt;/code&gt;，队列空间是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O(w)&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; 是树的最大宽度。&lt;/p&gt;

&lt;h2 id=&quot;和中序遍历放在一起看&quot;&gt;和中序遍历放在一起看&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;/leetcode/94-binary-tree-inorder-traversal/&quot;&gt;中序遍历&lt;/a&gt;是在合适的时机，把节点值加入结果列表；最大深度则需要接收左右子树的数字，再计算当前子树的数字。&lt;/p&gt;

&lt;p&gt;这两道题给我的启发是，写递归之前，要先问清楚：这个函数究竟负责什么？它是在记录经过的节点，还是要返回一个计算结果？把这件事说明白，终止条件、递归调用和返回值才容易接在一起。&lt;/p&gt;

&lt;p&gt;题目链接：&lt;a href=&quot;https://leetcode.cn/problems/maximum-depth-of-binary-tree/&quot;&gt;力扣 104 · 二叉树的最大深度&lt;/a&gt;。&lt;/p&gt;</content><author><name>wind989</name></author><category term="力扣刷题" /><summary type="html">我一开始想在递归之后给左右两边的变量加一。后来才理解，真正需要接收的是左右子树返回的深度，再取较大值加上当前这一层。</summary></entry><entry><title type="html">从提示词到任务本身：我对使用 AI 的一些思考</title><link href="https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/10/23/ai-task-thinking.html" rel="alternate" type="text/html" title="从提示词到任务本身：我对使用 AI 的一些思考" /><published>2026-10-23T00:00:00+08:00</published><updated>2026-10-23T00:00:00+08:00</updated><id>https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/10/23/ai-task-thinking</id><content type="html" xml:base="https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/10/23/ai-task-thinking.html">&lt;p&gt;最近读了&lt;a href=&quot;https://mp.weixin.qq.com/s/GrXmCxESkjQm-bovv0bZ3w&quot;&gt;《“大家用 AI 胆子还不够大！”Claude 工程负责人亲授顶级用法》&lt;/a&gt;，我最初的感想是：想让 AI 达到更好的效果，可以从四个角度完善提示词——标准、信息、工具和结果验证。&lt;/p&gt;

&lt;p&gt;把这四点记下来之后，我又顺着文章里提到的“高度”和“广度”想了想，发现这里面还有一个更靠前的问题：我交给 AI 的任务，究竟是不是自己真正想解决的问题？&lt;/p&gt;

&lt;h2 id=&quot;从四个角度完善任务&quot;&gt;从四个角度完善任务&lt;/h2&gt;

&lt;p&gt;先说最初想到的四点。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;标准，是先想清楚什么样的结果才算好。&lt;/strong&gt; 我们经常会对 AI 说“写得专业一点”“分析得深入一点”，但这些要求其实比较模糊。如果是写文章，写给谁看、想表达什么、哪些观点需要保留，都可以说得更具体。如果是解决问题，也应该尽量明确希望达到什么程度，以及怎样判断任务完成了。&lt;/p&gt;

&lt;p&gt;文章里有一点让我很有感触：要求 AI 扮演专家，可能只是让它学会了专家说话的样子。真正值得提出的要求，是让它采用专家解决问题时使用的方法。这样，“专业”才有了具体内容。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;信息，是让 AI 有足够的依据开展工作。&lt;/strong&gt; 背景、原始材料、限制条件，以及我们已经做过哪些尝试，都会影响它的判断。这里尤其值得注意的是原始信息。我们自己整理过的结论，可能已经带着某些假设。如果只把结论交给 AI，它就容易沿着我们的思路继续分析。提供原始记录，再让它检查这些结论是否成立，才有机会发现自己忽略的问题。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;工具，是让任务具备实际执行的条件。&lt;/strong&gt; 有些问题光靠对话很难处理，需要搜索资料、读取文件、分析数据，或者运行程序。提示词里可以要求 AI 使用合适的工具，但还得让它实际能够接触这些工具和数据。这也让我意识到，使用 AI 的效果，有时取决于我们为它准备了怎样的工作条件。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;结果验证，是检查它到底有没有把事情做好。&lt;/strong&gt; 一份回答逻辑顺畅、表达完整，很容易让人觉得它已经正确了。但事实能否核对，代码能否运行，分析能否经受新数据的检验，才是更可靠的判断依据。让 AI 自查可以有帮助，最终还是要尽量找到独立的证据。&lt;/p&gt;

&lt;p&gt;这四点主要回答的是：怎样让 AI 把一件事做得更扎实。&lt;/p&gt;

&lt;h2 id=&quot;高度从真正的目标出发&quot;&gt;高度：从真正的目标出发&lt;/h2&gt;

&lt;p&gt;而文章里的“高度”，让我开始思考应该交给 AI 什么事。&lt;/p&gt;

&lt;p&gt;文中举了季度业务复盘的例子。我们最容易想到的任务，是让 AI 帮忙做一份 PPT。但继续追问，做 PPT 是为了什么？可能是为了让管理层看清业务变化，发现问题，然后作出决定。既然如此，就可以把这个目标也告诉 AI，让它尝试分析变化、寻找原因、比较行动方案，甚至思考现有的复盘方式能否改进。&lt;/p&gt;

&lt;p&gt;我对“高度”的理解，就是多问一句：&lt;strong&gt;完成眼前这项任务之后，我真正希望发生什么？&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;最开始，我觉得可以先让 AI 完成一些表面或浅层的任务，再让它尝试更本质的任务。继续想下去，其实也不必有这个固定顺序。我们完全可以一开始就说明最终目标，再一起拆解需要做哪些工作。这样也更容易发现，原来自己默认的做法，只是实现目标的一条路径。&lt;/p&gt;

&lt;h2 id=&quot;广度借用其他领域的方法&quot;&gt;广度：借用其他领域的方法&lt;/h2&gt;

&lt;p&gt;“广度”则给了我另一个提醒：解决问题时，可以主动借用其他领域的方法。&lt;/p&gt;

&lt;p&gt;比如分析用户流失，可以用统计方法寻找规律，用访谈了解用户的真实原因，再通过实验检验改进是否有效。不同方法能回答不同的问题。重要的是知道引入它们分别是为了什么，而不是让 AI 多扮演几个角色，或者堆上更多工具名称。&lt;/p&gt;

&lt;p&gt;所以，我现在对这篇文章的理解，比最初的四点又多了一层：先想清楚真正的目标，再明确标准、提供信息、准备工具，并验证结果；过程中，也可以让 AI 寻找其他领域里适合当前问题的方法。&lt;/p&gt;

&lt;p&gt;这也让我重新理解了“完善提示词”这件事。写提示词的过程，其实也在检验我自己：目标想清楚了吗？依据充分吗？为什么采用这种做法？拿到结果后，准备怎样判断它是否有用？&lt;/p&gt;

&lt;p&gt;我想，用好 AI 的一个值得练习的能力，就是把这些问题想得更清楚，同时愿意让它尝试那些自己过去没有想到可以交给它的工作。&lt;/p&gt;</content><author><name>wind989</name></author><category term="学习记录" /><summary type="html">从标准、信息、工具和结果验证出发，思考如何给 AI 交代任务，以及怎样理解任务的高度和方法的广度。</summary></entry><entry><title type="html">我的第一篇技术博客</title><link href="https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/07/30/my-first-post.html" rel="alternate" type="text/html" title="我的第一篇技术博客" /><published>2026-07-30T00:00:00+08:00</published><updated>2026-07-30T00:00:00+08:00</updated><id>https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/07/30/my-first-post</id><content type="html" xml:base="https://wind9.xyz/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/2026/07/30/my-first-post.html">&lt;p&gt;这是我的第一篇博客文章。&lt;/p&gt;

&lt;p&gt;目前我正在学习：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Python&lt;/li&gt;
  &lt;li&gt;FastAPI&lt;/li&gt;
  &lt;li&gt;MySQL&lt;/li&gt;
  &lt;li&gt;SQLAlchemy&lt;/li&gt;
  &lt;li&gt;AI Agent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以后我会在这里记录我的学习过程、项目经验和遇到的问题。&lt;/p&gt;</content><author><name>wind989</name></author><category term="学习记录" /><summary type="html">建站之初的记录：从 Python、FastAPI、数据库到 AI Agent，整理自己的学习方向。</summary></entry></feed>