diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 67862680..756afb68 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -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)