Leetcode 404. Sum of Left Leaves
阿新 • • 發佈:2019-01-03
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isLeave(TreeNode* root) { if (!root) return false; if (!root->left && !root->right) return true; return false; } int sumOfLeftLeaves(TreeNode* root) { if (isLeave(root)) return 0; return sumOfLeftLeaves_(root); } int sumOfLeftLeaves_(TreeNode* root) { if (!root) return 0; if (isLeave(root)) return root->val; return sumOfLeftLeaves_(root->left) + ( isLeave(root->right) ? 0 : sumOfLeftLeaves_(root->right) ) ; } };