404. Sum of Left Leaves

来源:互联网 发布:mac dmg u盘 编辑:程序博客网 时间:2024/04/30 19:19

Find the sum of all left leaves in a given binary tree.

Example:

    3   / \  9  20    /  \   15   7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

明确左节点的定义:左节点没有子节点,  

root.left!=null&&root.left.left==null&&root.left.right==null   判断这个节点是左节点


public class Solution {    int count=0;    public int sumOfLeftLeaves(TreeNode root) {       order(root);       return count;    }    public void order(TreeNode root){        if(root==null) return ;        if(root.left!=null&&root.left.left==null&&root.left.right==null)            count+=root.left.val;        order(root.left);        order(root.right);        }}


0 0