1.左子叶之和
计算给定二叉树的所有左叶子之和。
示例:
3
/ \
9 20
/ \
15 7
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
思路:其实还是蛮简单的,搜索每个节点的最左子叶,并相加。
/**
* 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 sumOfLeftLeaves(TreeNode* root) {
int sumOfLeft=0;
stack<TreeNode*> stack;
int count=0;
int judgeRoot=1;
while(root || !stack.empty()){
while(root){
stack.push(root);
count=count+1;
int value=root->val;
judgeRoot=judgeRoot+1;
if(root->left==NULL && count>=2 &&root->right==NULL){