求所有左节点的和。

/**
* 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) { if(root==NULL)
return ;
else if(root->left!=NULL && root->left->left==NULL && root->left->right==NULL) //相当于递归的终止情形
return root->left->val+sumOfLeftLeaves(root->right);
else
return sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right);
//这个题分成以下几种情况进行讨论
//--如果当前节点是空,毫无疑问返回0
//--如果有左节点,但是左节点没有任何的子节点(左节点的值+右子树的和)
//--其他的情况:左子树和+右子树和 //傻孩子,这是你想的最好,但是错的最离谱的一次
/*
if (root == NULL || (root->left == NULL && root->right == NULL))
return 0;
if (root->left == NULL && root->right != NULL)
return sumOfLeftLeaves(root->right);
if (root->left != NULL && root->right == NULL)
return root->left->val + sumOfLeftLeaves(root->left);
if (root->left != NULL && root->right != NULL)
return root->left->val + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
*/
}
};

【easy】404. Sum of Left Leaves的更多相关文章

  1. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  2. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  3. 【Leetcode】【Easy】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  5. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  6. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  7. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  8. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  9. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

随机推荐

  1. 【转】JAVA多线程实现的四种方式

    原文地址:http://www.cnblogs.com/felixzh/p/6036074.html Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callabl ...

  2. 多项目管理中PMO的作用

    随着现代企业规模的不断扩大,多项目同时运行成为了现代企业的常态,以及企业项目化进程的不断深入,大部分企业不再仅仅只运行一个项目,而且数量之大已经超出了人们的想象,如惠普公司每年有3000个左右的项目, ...

  3. Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist

    当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...

  4. mybatis 使用事务处理

    mybatis默认开启事务 以前使用JDBC的时候,如果要开启事务,我们需要调用conn.setAutoCommit(false)方法来关闭自动提交,之后才能进行事务操作,否则每一次对数据库的操作都会 ...

  5. CodeForces 91B Queue

    题目链接:http://codeforces.com/contest/91/problem/B 题目大意: 有n头大象排队买票,第i头大象的年龄为ai,如果有比他年轻的大象排在他前面,这头大象就会非常 ...

  6. Android艺术——探究Handler运行机制

    我们从开发的角度来说,Handler是Android 的消息机制的上层接口.说到Handler,大家都会说:哦,Handler这个我知道干什么的,更新UI.没错,Handler的确是用于更新UI的,具 ...

  7. LODOP设置超文本不自动分页的方法

    在LODOP中,超文本超过打印项高度会自动分页,自动分页有两种情况:超过设置的打印项高度,超过纸张.这里是指高度,超过纸张宽度的超文本不会显示,会隐藏掉. 如果你不了解什么是LODOP中的超文本打印项 ...

  8. 【CF1132G】Greedy Subsequences(线段树)

    [CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入 ...

  9. centos 下安装显卡驱动步骤

    一. 先下载自己显卡对应的linux版本的驱动文件, 一般都是.run的一个文件. 二.如果是新安装的系统,先安装编译环境,gcc,kernel-devel,kernel-headers  (联网) ...

  10. [FJOI2016]建筑师

    题目描述 小 Z 是一个很有名的建筑师,有一天他接到了一个很奇怪的任务:在数轴上建 n 个建筑,每个建筑的高度是 1 到 n 之间的一个整数. 小 Z 有很严重的强迫症,他不喜欢有两个建筑的高度相同. ...