求树的高度

    xiaoxiao2022-07-04  173

    /**

    public class TreeNode {

    int val = 0;

    TreeNode left = null;

    TreeNode right = null;

    public TreeNode(int val) {

    this.val = val;

    }

    }

    */

    public class Solution { public int TreeDepth(TreeNode root) { if(root==null){ return 0; } //如果当前节点不为null,深度加1,再递归求出左右子树的深度,然后取最大值,即为树的深度 return Math.max(1+TreeDepth(root.left),1+TreeDepth(root.right)); } }

    最新回复(0)