leetcode 404. Sum of Left Leaves

来源:互联网 发布:承接大数据项目外包 编辑:程序博客网 时间:2024/05/02 23:15
/** * 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:    int ans;    void dfs(TreeNode * root,int l){        if(root == NULL) return;        if(root->left == NULL && root->right == NULL){            if(l == 1) ans += root->val;            return ;        }        dfs(root->left,1);        dfs(root->right,0);    }    int sumOfLeftLeaves(TreeNode* root) {        ans = 0;         dfs(root,0);         return ans;    }};

0 0
原创粉丝点击