mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -292,6 +292,44 @@ var maxDepth = function(root) {
|
||||
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right))
|
||||
};
|
||||
```
|
||||
二叉树最大深度递归遍历
|
||||
```javascript
|
||||
var maxDepth = function(root) {
|
||||
//使用递归的方法 递归三部曲
|
||||
//1. 确定递归函数的参数和返回值
|
||||
const getDepth=function(node){
|
||||
//2. 确定终止条件
|
||||
if(node===null){
|
||||
return 0;
|
||||
}
|
||||
//3. 确定单层逻辑
|
||||
let leftDepth=getDepth(node.left);
|
||||
let rightDepth=getDepth(node.right);
|
||||
let depth=1+Math.max(leftDepth,rightDepth);
|
||||
return depth;
|
||||
}
|
||||
return getDepth(root);
|
||||
};
|
||||
```
|
||||
二叉树最大深度层级遍历
|
||||
```javascript
|
||||
var maxDepth = function(root) {
|
||||
//使用递归的方法 递归三部曲
|
||||
//1. 确定递归函数的参数和返回值
|
||||
const getDepth=function(node){
|
||||
//2. 确定终止条件
|
||||
if(node===null){
|
||||
return 0;
|
||||
}
|
||||
//3. 确定单层逻辑
|
||||
let leftDepth=getDepth(node.left);
|
||||
let rightDepth=getDepth(node.right);
|
||||
let depth=1+Math.max(leftDepth,rightDepth);
|
||||
return depth;
|
||||
}
|
||||
return getDepth(root);
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
Reference in New Issue
Block a user