首页 > 代码库 > 树的最大深度 leecode java

树的最大深度 leecode java

秒杀
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; int h1=maxDepth(root.left); int h2=maxDepth(root.right); return h1>h2?h1+1:h2+1; }}