【easy】404. Sum of Left Leaves
求所有左节点的和。
/**
* 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的更多相关文章
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【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 ...
- 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 ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 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 ...
- 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 ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- SQL 无法连接服务器
错误信息:provider:SQL Network Interfaces, error:52-无法定位 LOCA Database Runtime 安装.请验证SQL Server Express是否 ...
- OpenStack-Glance(3)
一. Glance功能 传统 IT 环境下,安装一个系统是要么从CD安装,要么用 Ghost 等克隆工具恢复.有如下几个问题: 如果要安装的系统多了效率就很低 时间长,工作量大 安装完还要进行手工配置 ...
- Open Source
资源来源于http://www.cnblogs.com/Leo_wl/category/246424.html RabbitMQ 安装与使用 摘要: RabbitMQ 安装与使用 前言 吃多了拉就是队 ...
- 数据库和SQL面试题基础知识(持续更新)
数据库方面基础知识复习 常问小问题: 一.like查询大小写问题: sql查询结果去重 SELECT distinct name FROM 表:平均数avg 一.like查询大小写问题: ①用bina ...
- Laravel 入口文件解读及生命周期
这里只贴index.php的代码, 深入了解的请访问 https://laravel-china.org/articles/10421/depth-mining-of-laravel-life- ...
- python之常用模块一(time、random、os、sys)
摘要:时间模块time .随机模块random .os模块.sys模块 一.时间模块 三种格式 时间戳时间:浮点数 单位为秒 时间戳起始时间: 1970.1.1 0:0:0 英国伦敦时间 1970.1 ...
- Linux saltstack常用模块
所有模块 salt '172.30.100.126' sys.list_modules #列出当前版本支持的模块 salt '*' sys.doc cp #显示指定模块的文档 archive模块 实现 ...
- VirtualBox下安装linux虚拟机
下载VirtualBox 下载地址:https://www.virtualbox.org/wiki/Downloads 安装VirtualBox 安装虚拟机 如果选择不到64位系统, 开机按 F1 进 ...
- deepin安装mysql记录
本文转载自http://www.linuxidc.com/Linux/2016-07/133128.htm sudo apt-get install mysql-server apt-get isnt ...
- Stacking:Catboost、Xgboost、LightGBM、Adaboost、RF etc
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...