LeetCode404:
    计算给定二叉树的所有左叶子之和。

    1. class Solution {
    2. int sum = 0;
    3. public int sumOfLeftLeaves(TreeNode root) {
    4. if (root == null) {
    5. return sum;
    6. }
    7. preOrder(root);
    8. return sum;
    9. }
    10. private void preOrder(TreeNode root) {
    11. if (root == null) {
    12. return;
    13. }
    14. if (root.left != null) {
    15. if (root.left.left == null && root.left.right == null){
    16. sum += root.left.val;
    17. }
    18. preOrder(root.left);
    19. }
    20. if (root.right != null) {
    21. preOrder(root.right);
    22. }
    23. }
    24. }