<?php
/*class TreeNode{
var $val;
var $left = NULL;
var $right = NULL;
function __construct($val){
$this->val = $val;
}
}*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param pRoot TreeNode类
* @return int整型
*/
function TreeDepth( $pRoot )
{
// write code here
if ($pRoot == null) {
return 0;
}
return 1+ max(TreeDepth($pRoot->left), TreeDepth($pRoot->right));
}
动态规划的例子,思路:要找整个树的最大深度,那就求左右子树中的较大的深度,再加上根节点的深度1.