使用动态规划

public class Solution {
    private int ret=0;
    public int TreeDepth(TreeNode root) {
        dfs(root,0);
        return ret;
    }
    public void dfs(TreeNode root,int depth)
    {
        if(root!=null)
        {
            ret=Math.max(ret,++depth);
            dfs(root.left,depth);
            dfs(root.right,depth);
        }
    }
}