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