思路: 判断是不是叶子节点 右节点中也有左叶子节点 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int sumOfLeftLeaves(TreeNode root) { return root != null ? dfs(root) : 0; } public int dfs(TreeNode node) { int ans = 0; if (node.left != null) { ans += isLeafNode(node.left) ? node.left.val : dfs(node.left); } if (node.right != null && !isLeafNode(node.right)) { ans += dfs(node.right); } return ans; } public boolean isLeafNode(TreeNode node) { return node.left == null && node.right == null; } } class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root==null) return 0; return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right) + (root.left!=null && root.left.left==null && root.left.right==null ? root.left.val : 0); } }