Merge pull request #248 from xllpiupiu/master

二叉树最大深度JavaScript版本
This commit is contained in:
Carl Sun
2021-05-25 18:21:54 +08:00
committed by GitHub

View File

@ -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)