LeetCode之17. Letter Combinations of a Phone Number

博客展示了一段Java代码在解决二叉树最小深度问题上的性能表现,运行时间为0 ms,超过100.00%的在线提交,内存使用40.2 MB,优于5.41%的在线提交。

 

自己的代码:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Depth of Binary Tree.

Memory Usage: 40.2 MB, less than 5.41% of Java online submissions for Minimum Depth of Binary 

public static int minDepth(TreeNode root) {
	if(root == null) return 0;
	int[] result = new int[1]; // 全局变量,更新最小值
	result[0] = Integer.MAX_VALUE; // 求最小值,则需要预置Integer.MAX_VALUE
	
	depthHelper(root,1,result);

	return result[0];
}

public static void depthHelper(TreeNode root,int depth,int[] result) {
	if(root != null && root.left == null && root.right == null) { // 必须为叶子节点
		if( depth<result[0] ) {
			result[0] = depth;
		}
		return;
	}
	
	if(root.left != null) { // 非空判断
		depthHelper(root.left,depth+1,result);
	}
	if(root.right != null) {
		depthHelper(root.right,depth+1,result);
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值