# 0104. 二叉树的最大深度 ## 题目地址(104. 二叉树的最大深度) <https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/> ## 题目描述 ``` <pre class="calibre18">``` 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。 ``` ``` ## 前置知识 - [递归](https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md) ## 公司 - 阿里 - 腾讯 - 百度 - 字节 - apple - linkedin - uber - yahoo ## 思路 由于树是一种递归的数据结构,因此用递归去解决的时候往往非常容易,这道题恰巧也是如此, 用递归实现的代码如下: ``` <pre class="calibre18">``` <span class="hljs-keyword">var</span> maxDepth = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">root</span>) </span>{ <span class="hljs-keyword">if</span> (!root) <span class="hljs-keyword">return</span> <span class="hljs-params">0</span>; <span class="hljs-keyword">if</span> (!root.left && !root.right) <span class="hljs-keyword">return</span> <span class="hljs-params">1</span>; <span class="hljs-keyword">return</span> <span class="hljs-params">1</span> + <span class="hljs-params">Math</span>.max(maxDepth(root.left), maxDepth(root.right)); }; ``` ``` 如果使用迭代呢? 我们首先应该想到的是树的各种遍历,由于我们求的是深度,因此 使用层次遍历(BFS)是非常合适的。 我们只需要记录有多少层即可。相关思路请查看[binary-tree-traversal](binary-tree-traversal.html) ## 关键点解析 - 队列 - 队列中用 Null(一个特殊元素)来划分每层,或者在对每层进行迭代之前保存当前队列元素的个数(即当前层所含元素个数) - 树的基本操作- 遍历 - 层次遍历(BFS) ## 代码 - 语言支持:JS,C++,Python JavaScript Code: ``` <pre class="calibre18">``` <span class="hljs-title">/* * @lc app=leetcode id=104 lang=javascript * * [104] Maximum Depth of Binary Tree */</span> <span class="hljs-title">/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */</span> <span class="hljs-title">/** * @param {TreeNode} root * @return {number} */</span> <span class="hljs-keyword">var</span> maxDepth = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">root</span>) </span>{ <span class="hljs-keyword">if</span> (!root) <span class="hljs-keyword">return</span> <span class="hljs-params">0</span>; <span class="hljs-keyword">if</span> (!root.left && !root.right) <span class="hljs-keyword">return</span> <span class="hljs-params">1</span>; <span class="hljs-title">// 层次遍历 BFS</span> <span class="hljs-keyword">let</span> cur = root; <span class="hljs-keyword">const</span> queue = [root, <span class="hljs-params">null</span>]; <span class="hljs-keyword">let</span> depth = <span class="hljs-params">1</span>; <span class="hljs-keyword">while</span> ((cur = queue.shift()) !== <span class="hljs-params">undefined</span>) { <span class="hljs-keyword">if</span> (cur === <span class="hljs-params">null</span>) { <span class="hljs-title">// 注意⚠️: 不处理会无限循环,进而堆栈溢出</span> <span class="hljs-keyword">if</span> (queue.length === <span class="hljs-params">0</span>) <span class="hljs-keyword">return</span> depth; depth++; queue.push(<span class="hljs-params">null</span>); <span class="hljs-keyword">continue</span>; } <span class="hljs-keyword">const</span> l = cur.left; <span class="hljs-keyword">const</span> r = cur.right; <span class="hljs-keyword">if</span> (l) queue.push(l); <span class="hljs-keyword">if</span> (r) queue.push(r); } <span class="hljs-keyword">return</span> depth; }; ``` ``` C++ Code: ``` <pre class="calibre18">``` <span class="hljs-title">/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */</span> <span class="hljs-keyword">class</span> Solution { <span class="hljs-keyword">public</span>: <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">maxDepth</span><span class="hljs-params">(TreeNode* root)</span> </span>{ <span class="hljs-keyword">if</span> (root == <span class="hljs-params">nullptr</span>) <span class="hljs-keyword">return</span> <span class="hljs-params">0</span>; <span class="hljs-keyword">auto</span> q = <span class="hljs-params">vector</span><TreeNode*>(); <span class="hljs-keyword">auto</span> d = <span class="hljs-params">0</span>; q.push_back(root); <span class="hljs-keyword">while</span> (!q.empty()) { ++d; <span class="hljs-keyword">auto</span> sz = q.size(); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i = <span class="hljs-params">0</span>; i < sz; ++i) { <span class="hljs-keyword">auto</span> t = q.front(); q.erase(q.begin()); <span class="hljs-keyword">if</span> (t->left != <span class="hljs-params">nullptr</span>) q.push_back(t->left); <span class="hljs-keyword">if</span> (t->right != <span class="hljs-params">nullptr</span>) q.push_back(t->right); } } <span class="hljs-keyword">return</span> d; } }; ``` ``` Python Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">maxDepth</span><span class="hljs-params">(self, root: TreeNode)</span> -> int:</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> root: <span class="hljs-keyword">return</span> <span class="hljs-params">0</span> q, depth = [root, <span class="hljs-keyword">None</span>], <span class="hljs-params">1</span> <span class="hljs-keyword">while</span> q: node = q.pop(<span class="hljs-params">0</span>) <span class="hljs-keyword">if</span> node: <span class="hljs-keyword">if</span> node.left: q.append(node.left) <span class="hljs-keyword">if</span> node.right: q.append(node.right) <span class="hljs-keyword">elif</span> q: q.append(<span class="hljs-keyword">None</span>) depth += <span class="hljs-params">1</span> <span class="hljs-keyword">return</span> depth ``` ``` **复杂度分析** - 时间复杂度:O(N)O(N)O(N) - 空间复杂度:O(N)O(N)O(N) ## 相关题目 - [102.binary-tree-level-order-traversal](102.binary-tree-level-order-traversal.html) - [103.binary-tree-zigzag-level-order-traversal](103.binary-tree-zigzag-level-order-traversal.html) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:<https://github.com/azl397985856/leetcode> 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://img.kancloud.cn/cf/0f/cf0fc0dd21e94b443dd8bca6cc15b34b_900x500.jpg)