求二叉树深度

tech2025-08-28  6

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int maxDepth(TreeNode root) { return recurr(root); } int recurr(TreeNode root){ if(root==null) return 0; int l = recurr(root.left)+1; int r = recurr(root.right)+1; return Math.max(l,r); } }
最新回复(0)