计算给定二叉树的左叶子之和。
树上叶子节点指没有后继节点的节点,左叶子指连向父节点的左侧的叶子节点。
样例 2 解释:
叶子节点有 4 , 5 ,3,左叶子只有 4 ,所以答案返回 4
样例 3 解释
叶子节点有 4 , 5 ,6,左叶子有 4 , 6,所以答案返回 10
数据范围:树上节点的数量满足 ,节点上的值满足
{1,2}
2
{1,2,3,4,5}
4
{1,2,3,4,5,6}
10
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int res = 0; public int sumOfLeftLeaves (TreeNode root) { // write code here if (root == null) return 0; preOrder(root); return res; } private void preOrder(TreeNode t) { if (t != null) { if (t.left != null && t.left.left == null && t.left.right == null) { res = res + t.left.val; } preOrder(t.left); preOrder(t.right); } } }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ public int sumOfLeftLeaves (TreeNode root) { if (root == null) { return 0; } if (root.left != null) { if (root.left.left == null && root.left.right == null) { // 叶子节点 return root.left.val + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); } } return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); } }
public class Solution { public int sumOfLeftLeaves (TreeNode root) { if (root == null) { return 0; } TreeNode left = root.left; if (left != null && left.left == null && left.right == null) { return left.val + this.sumOfLeftLeaves(root.right); } return this.sumOfLeftLeaves(root.left) + this.sumOfLeftLeaves(root.right); } }
import java.util.*; public class Solution { public int sumOfLeftLeaves (TreeNode root) { if (root == null) { return 0; } if (isLeaf(root.left)) { //若节点左孩子为叶节点,返回其值并加上右子树中左叶子的值 return root.left.val + sumOfLeftLeaves(root.right); } else { //否则返回该节点左子树左叶子的值加上右子树左叶子的值 return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right); } } private boolean isLeaf(TreeNode root) { //判断节点是否为叶节点 if (root == null) { return false; } return root.left == null && root.right == null; } }
public class Solution { public int sumOfLeftLeaves (TreeNode root) { dfs(root, true); return cnt; } public int cnt = 0; public void dfs(TreeNode root, boolean flag) { if (root == null) return; if (root.left == null && root.right == null && flag) { cnt += root.val; } dfs(root.left, true); dfs(root.right, false); } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ public int sumOfLeftLeaves (TreeNode root) { // write code here int sum = 0; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()){ TreeNode node = queue.poll(); if(node.left != null){ if(node.left.left == null && node.left.right == null){ sum += node.left.val; // 这是个左叶子节点,累加上它的值 } queue.offer(node.left); } if(node.right != null){ queue.offer(node.right); } } return sum; } }