mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 0104.二叉树的最大深度: 调整一段 js 代码格式
This commit is contained in:
@ -59,7 +59,7 @@ int getdepth(treenode* node)
|
||||
if (node == NULL) return 0;
|
||||
```
|
||||
|
||||
3. 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
|
||||
3. 确定单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
|
||||
|
||||
代码如下:
|
||||
|
||||
@ -591,15 +591,15 @@ var maxdepth = function(root) {
|
||||
var maxdepth = function(root) {
|
||||
//使用递归的方法 递归三部曲
|
||||
//1. 确定递归函数的参数和返回值
|
||||
const getdepth=function(node){
|
||||
const getdepth = function(node) {
|
||||
//2. 确定终止条件
|
||||
if(node===null){
|
||||
if(node === null) {
|
||||
return 0;
|
||||
}
|
||||
//3. 确定单层逻辑
|
||||
let leftdepth=getdepth(node.left);
|
||||
let rightdepth=getdepth(node.right);
|
||||
let depth=1+Math.max(leftdepth,rightdepth);
|
||||
let leftdepth = getdepth(node.left);
|
||||
let rightdepth = getdepth(node.right);
|
||||
let depth = 1 + Math.max(leftdepth, rightdepth);
|
||||
return depth;
|
||||
}
|
||||
return getdepth(root);
|
||||
|
Reference in New Issue
Block a user