• leetcode 404 左叶子之和


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

    示例:

    3
    /
    9 20
    /
    15 7

    在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/sum-of-left-leaves
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    代码:

    class Solution {
    public:
        int sumOfLeftLeaves(TreeNode* root) {
            int sum = 0;
            if(root == NULL) return 0;
            if(root -> left) {
                TreeNode *temp = root -> left;
                if(temp -> left == NULL && temp -> right == NULL) {
                    sum += temp -> val;
                }
                sum += sumOfLeftLeaves(root -> left);
            }
            if(root -> right) {
                sum += sumOfLeftLeaves(root -> right);
            }
            return sum;
        }
    };
    如果觉得有帮助,点个推荐啦~
  • 相关阅读:
    Numpy
    啊大大阿达
    asda
    啊大大
    初识python
    初识python
    初识python
    初识python
    初识python
    初识python
  • 原文地址:https://www.cnblogs.com/8023spz/p/13694598.html
Copyright © 2020-2023  润新知