树、递归————左子树之和

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int sumOfLeftLeaves(TreeNode* root) {
13         int res=0;
14         if(root==NULL) return res;
15         //单节点
16         if(!root->left && !root->right) return res;
17         //到达左叶子节点
18         if(root->left && !root->left->left && !root->left->right) res+= root->left->val;
19         else if(root->left) res+=sumOfLeftLeaves(root->left);
20         if(root->right) res+=sumOfLeftLeaves(root->right);
21         return res;
22     }
23 };

 

posted @ 2019-06-24 15:36  Austin_anheqiao  阅读(290)  评论(0编辑  收藏  举报