题解 | #二叉树的最大深度#
二叉树的最大深度
https://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @return int整型
*/
function maxDepth( root ) {
// write code here
if(!root) return 0;
const left = maxDepth(root.left);
const right = maxDepth(root.right);
return left > right ? left + 1 : right + 1;
}
module.exports = {
maxDepth : maxDepth
};
二叉树的题用递归解法会比较简单
求树的深度,节点有左右子树就取最大的那边+1
