leetcode.404. 左叶子之和
作者:互联网
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
示例 2:
输入: root = [1]
输出: 0
提示:
节点数在 [1, 1000] 范围内
-1000 <= Node.val <= 1000
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public int sumOfLeftLeaves(TreeNode root) { int res=0; if(root==null)return 0; if(root.left!=null&&root.left.left==null&&root.left.right==null) res=root.left.val; return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right)+res; } }
标签:right,TreeNode,val,叶子,leetcode.404,null,root,left 来源: https://www.cnblogs.com/15078480385zyc/p/16482720.html