NC248 左叶子之和
计算给定二叉树的左叶子之和。
树上叶子节点指没有后继节点的节点,左叶子指连向父节点的左侧的叶子节点。
int sumOfLeftLeaves(struct TreeNode* root ) {if (root != NULL) {int sum = 0;if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) {sum = root->left->val;}return sum + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);}return 0;
}
int sumOfLeftLeaves(struct TreeNode* root) {if (root != NULL) {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);}return 0;
}