404.递归求左叶子之和

题目描述
题目描述

代码

public class Solution {
	public int sumOfLeftLeaves(TreeNode root){
		int sum = 0;
		if(root == null){
			return 0;//这个判断非常重要,若缺失将引发空指针异常
		}
		if(root.left!=null && root.left.left==null && root.left.right == null){
			sum =root.left.val;
		}else{
			sum += sumOfLeftLeaves(root.left);
		}
		sum += sumOfLeftLeaves(root.right);
		return sum;
	}
}

性能表现
性能表现

发布了75 篇原创文章 · 获赞 0 · 访问量 1511

猜你喜欢

转载自blog.csdn.net/qq_34087914/article/details/104100501
今日推荐