左と葉の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