标准的树递归,不多说了

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    static int high=0;
    public int TreeDepth(TreeNode root) {
        fun(root,0);
        return high;
    }
    public void fun(TreeNode head,int h){
        if(head==null){
            return;
        }
        int now_h=h+1;
        //如果当前是叶子节点
        if(head.left==null&&head.right==null){
            if(now_h>high){
                high=now_h;
                return;
            }
        }
        //如果当前不是叶子节点,当前深度+1,传给孩子
        if(head.left != null || head.right != null){
            fun(head.left,now_h);
            fun(head.right,now_h);
        }
    }
}