404. Sum of Left Leaves

    xiaoxiao2021-03-25  136

        没有什么好说的,求二叉树所有左叶子节点的值

    /**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode(int x) { val = x; }  * }  */ public class Solution {     public int sumOfLeftLeaves(TreeNode root) {         if (root == null)             return 0;         int sum = 0;         if (root.left != null && root.left.left == null && root.left.right == null) {             sum += root.left.val;         } else {             sum += sumOfLeftLeaves(root.left);         }         sum += sumOfLeftLeaves(root.right);         return sum;     } }

    转载请注明原文地址: https://ju.6miu.com/read-9552.html

    最新回复(0)