[leetcode]: 404. Sum of Left Leaves

来源:互联网 发布:37龙神契约进阶数据 编辑:程序博客网 时间:2024/05/29 03:41

1.题目描述

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

Example:

 3/  \9  20   /  \  15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
求一棵二叉树所有左叶子节点的和

2.分析

注意:1)左 2)叶子结点

3.代码

int sumOfLeftLeaves(TreeNode* root) {    if (root == NULL)        return 0;    if(root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL)//左节点是叶子节点        return root->left->val+ sumOfLeftLeaves(root->right);    return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);    }
原创粉丝点击