数码资讯
力扣简104 二叉树的最大深度
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
自己刚看了前两题的递归就去写了!所以一下就过了!但是内存有点垃圾哈哈哈。
package leetcode01; public class Solution104 { public static int maxDepth(TreeNode root) { int depth=0; return Depth(root, depth); } public static int Depth(TreeNode root,int depth) { if(root==null) { return depth; } else { depth++; return Math.max(Depth(root.left, depth), Depth(root.right, depth)); } } public static void main(String[] args) { // TODO Auto-generated method stub TreeNode root=new TreeNode(1,new TreeNode(2,new TreeNode(1),null),null); System.out.print(maxDepth(root)); } }
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。