mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
refactor: 0102.二叉树的层序遍历
This commit is contained in:
@ -2809,21 +2809,23 @@ func maxDepth(root *TreeNode) int {
|
|||||||
* @param {TreeNode} root
|
* @param {TreeNode} root
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
var maxDepth = function(root) {
|
var maxDepth = function (root) {
|
||||||
// 最大的深度就是二叉树的层数
|
// 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
|
||||||
if (root === null) return 0;
|
let max = 0,
|
||||||
let queue = [root];
|
queue = [root];
|
||||||
let height = 0;
|
if (root === null) {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
while (queue.length) {
|
while (queue.length) {
|
||||||
let n = queue.length;
|
max++;
|
||||||
height++;
|
let length = queue.length;
|
||||||
for (let i=0; i<n; i++) {
|
while (length--) {
|
||||||
let node = queue.shift();
|
let node = queue.shift();
|
||||||
node.left && queue.push(node.left);
|
node.left && queue.push(node.left);
|
||||||
node.right && queue.push(node.right);
|
node.right && queue.push(node.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return height;
|
return max;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user