2024年5月27日 星期一

[404] Sum of Left Leaves

暖身題也搞了好半天Orz tree真難ToT

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int dfs(struct TreeNode* root)
{
if (root==NULL)
return 0;

if (root->left != NULL && root->left->left == NULL && root->left->right == NULL)
return root->left->val + dfs(root->left) + dfs(root->right);
return dfs(root->left)+dfs(root->right);
}

int sumOfLeftLeaves(struct TreeNode* root) {
return dfs(root);
}

沒有留言:

張貼留言