|
| 1 | + |
| 2 | +<!-- problem:start --> |
| 3 | + |
| 4 | +# [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree) |
| 5 | + |
| 6 | +--- |
| 7 | +- **comments**: true |
| 8 | +- **difficulty**: Easy |
| 9 | +- **edit_url**: https://github.com/doocs/leetcode/edit/main/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md |
| 10 | +- **tags**: |
| 11 | + - Tree |
| 12 | + - Depth-First Search |
| 13 | + - Breadth-First Search |
| 14 | + - Binary Tree |
| 15 | +--- |
| 16 | + |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +<!-- description:start --> |
| 21 | + |
| 22 | +<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p> |
| 23 | + |
| 24 | +<p>A binary tree's <strong>maximum depth</strong> is the number of nodes along the longest path from the root node down to the farthest leaf node.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/images/tmp-tree.jpg" style="width: 400px; height: 277px;" /> |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> root = [3,9,20,null,null,15,7] |
| 31 | +<strong>Output:</strong> 3 |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> root = [1,null,2] |
| 38 | +<strong>Output:</strong> 2 |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li> |
| 46 | + <li><code>-100 <= Node.val <= 100</code></li> |
| 47 | +</ul> |
| 48 | + |
| 49 | +<!-- description:end --> |
| 50 | + |
| 51 | +## Solutions |
| 52 | + |
| 53 | +<!-- solution:start --> |
| 54 | + |
| 55 | +### Solution 1: Recursion |
| 56 | + |
| 57 | +Recursively traverse the left and right subtrees, calculate the maximum depth of the left and right subtrees, and then take the maximum value plus $1$. |
| 58 | + |
| 59 | +The time complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. Each node is traversed only once in the recursion. |
| 60 | + |
| 61 | +<!-- tabs:start --> |
| 62 | + |
| 63 | +#### Python3 |
| 64 | + |
| 65 | +```python |
| 66 | +# Definition for a binary tree node. |
| 67 | +# class TreeNode: |
| 68 | +# def __init__(self, val=0, left=None, right=None): |
| 69 | +# self.val = val |
| 70 | +# self.left = left |
| 71 | +# self.right = right |
| 72 | +class Solution: |
| 73 | + def maxDepth(self, root: TreeNode) -> int: |
| 74 | + if root is None: |
| 75 | + return 0 |
| 76 | + l, r = self.maxDepth(root.left), self.maxDepth(root.right) |
| 77 | + return 1 + max(l, r) |
| 78 | +``` |
| 79 | + |
| 80 | +#### Java |
| 81 | + |
| 82 | +```java |
| 83 | +/** |
| 84 | + * Definition for a binary tree node. |
| 85 | + * public class TreeNode { |
| 86 | + * int val; |
| 87 | + * TreeNode left; |
| 88 | + * TreeNode right; |
| 89 | + * TreeNode() {} |
| 90 | + * TreeNode(int val) { this.val = val; } |
| 91 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 92 | + * this.val = val; |
| 93 | + * this.left = left; |
| 94 | + * this.right = right; |
| 95 | + * } |
| 96 | + * } |
| 97 | + */ |
| 98 | +class Solution { |
| 99 | + public int maxDepth(TreeNode root) { |
| 100 | + if (root == null) { |
| 101 | + return 0; |
| 102 | + } |
| 103 | + int l = maxDepth(root.left); |
| 104 | + int r = maxDepth(root.right); |
| 105 | + return 1 + Math.max(l, r); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +#### C++ |
| 111 | + |
| 112 | +```cpp |
| 113 | +/** |
| 114 | + * Definition for a binary tree node. |
| 115 | + * struct TreeNode { |
| 116 | + * int val; |
| 117 | + * TreeNode *left; |
| 118 | + * TreeNode *right; |
| 119 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 120 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 121 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 122 | + * }; |
| 123 | + */ |
| 124 | +class Solution { |
| 125 | +public: |
| 126 | + int maxDepth(TreeNode* root) { |
| 127 | + if (!root) return 0; |
| 128 | + int l = maxDepth(root->left), r = maxDepth(root->right); |
| 129 | + return 1 + max(l, r); |
| 130 | + } |
| 131 | +}; |
| 132 | +``` |
| 133 | +
|
| 134 | +#### Go |
| 135 | +
|
| 136 | +```go |
| 137 | +/** |
| 138 | + * Definition for a binary tree node. |
| 139 | + * type TreeNode struct { |
| 140 | + * Val int |
| 141 | + * Left *TreeNode |
| 142 | + * Right *TreeNode |
| 143 | + * } |
| 144 | + */ |
| 145 | +func maxDepth(root *TreeNode) int { |
| 146 | + if root == nil { |
| 147 | + return 0 |
| 148 | + } |
| 149 | + l, r := maxDepth(root.Left), maxDepth(root.Right) |
| 150 | + return 1 + max(l, r) |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +#### TypeScript |
| 155 | + |
| 156 | +```ts |
| 157 | +/** |
| 158 | + * Definition for a binary tree node. |
| 159 | + * class TreeNode { |
| 160 | + * val: number |
| 161 | + * left: TreeNode | null |
| 162 | + * right: TreeNode | null |
| 163 | + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { |
| 164 | + * this.val = (val===undefined ? 0 : val) |
| 165 | + * this.left = (left===undefined ? null : left) |
| 166 | + * this.right = (right===undefined ? null : right) |
| 167 | + * } |
| 168 | + * } |
| 169 | + */ |
| 170 | + |
| 171 | +function maxDepth(root: TreeNode | null): number { |
| 172 | + if (root === null) { |
| 173 | + return 0; |
| 174 | + } |
| 175 | + return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +#### Rust |
| 180 | + |
| 181 | +```rust |
| 182 | +// Definition for a binary tree node. |
| 183 | +// #[derive(Debug, PartialEq, Eq)] |
| 184 | +// pub struct TreeNode { |
| 185 | +// pub val: i32, |
| 186 | +// pub left: Option<Rc<RefCell<TreeNode>>>, |
| 187 | +// pub right: Option<Rc<RefCell<TreeNode>>>, |
| 188 | +// } |
| 189 | +// |
| 190 | +// impl TreeNode { |
| 191 | +// #[inline] |
| 192 | +// pub fn new(val: i32) -> Self { |
| 193 | +// TreeNode { |
| 194 | +// val, |
| 195 | +// left: None, |
| 196 | +// right: None |
| 197 | +// } |
| 198 | +// } |
| 199 | +// } |
| 200 | +use std::cell::RefCell; |
| 201 | +use std::rc::Rc; |
| 202 | +impl Solution { |
| 203 | + fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 204 | + if root.is_none() { |
| 205 | + return 0; |
| 206 | + } |
| 207 | + let node = root.as_ref().unwrap().borrow(); |
| 208 | + 1 + Self::dfs(&node.left).max(Self::dfs(&node.right)) |
| 209 | + } |
| 210 | + |
| 211 | + pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 212 | + Self::dfs(&root) |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +#### JavaScript |
| 218 | + |
| 219 | +```js |
| 220 | +/** |
| 221 | + * Definition for a binary tree node. |
| 222 | + * function TreeNode(val, left, right) { |
| 223 | + * this.val = (val===undefined ? 0 : val) |
| 224 | + * this.left = (left===undefined ? null : left) |
| 225 | + * this.right = (right===undefined ? null : right) |
| 226 | + * } |
| 227 | + */ |
| 228 | +/** |
| 229 | + * @param {TreeNode} root |
| 230 | + * @return {number} |
| 231 | + */ |
| 232 | +var maxDepth = function (root) { |
| 233 | + if (!root) return 0; |
| 234 | + const l = maxDepth(root.left); |
| 235 | + const r = maxDepth(root.right); |
| 236 | + return 1 + Math.max(l, r); |
| 237 | +}; |
| 238 | +``` |
| 239 | + |
| 240 | +#### C |
| 241 | + |
| 242 | +```c |
| 243 | +/** |
| 244 | + * Definition for a binary tree node. |
| 245 | + * struct TreeNode { |
| 246 | + * int val; |
| 247 | + * struct TreeNode *left; |
| 248 | + * struct TreeNode *right; |
| 249 | + * }; |
| 250 | + */ |
| 251 | + |
| 252 | +#define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 253 | + |
| 254 | +int maxDepth(struct TreeNode* root) { |
| 255 | + if (!root) { |
| 256 | + return 0; |
| 257 | + } |
| 258 | + int left = maxDepth(root->left); |
| 259 | + int right = maxDepth(root->right); |
| 260 | + return 1 + max(left, right); |
| 261 | +} |
| 262 | +``` |
| 263 | +
|
| 264 | +<!-- tabs:end --> |
| 265 | +
|
| 266 | +<!-- solution:end --> |
| 267 | +
|
| 268 | +<!-- problem:end --> |
0 commit comments