二叉树最大深度。题意很简单,给一个二叉树,求它的最大深度。这是后序遍历的基础题,直接上代码。
Example:
Given binary tree
[3,9,20,null,null,15,7]
,3 / \ 9 20 / \ 15 7return its depth = 3.
时间O(n)
空间O(h) - 树的高度
1 /** 2 * @param {TreeNode} root 3 * @return {number} 4 */ 5 var maxDepth = function(root) { 6 if (root === null) return 0; 7 let leftMax = maxDepth(root.left); 8 let rightMax = maxDepth(root.right); 9 return Math.max(leftMax, rightMax) + 1; 10 };
来源:https://www.cnblogs.com/aaronliu1991/p/12159329.html