求所有左节点的和。

/**
* 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. SpringCloud(6)分布式配置中心Spring Cloud Config

    1.Spring Cloud Config 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组 ...

  2. Swagger 配置

    放行 , 不拦截. @Configuration open class MyWebMvcConfig : WebMvcConfigurationSupport() { override fun add ...

  3. 基于C#的socket编程的TCP异步实现

    一.摘要 本篇博文阐述基于TCP通信协议的异步实现. 二.实验平台 Visual Studio 2010 三.异步通信实现原理及常用方法 3.1 建立连接 在同步模式中,在服务器上使用Accept方法 ...

  4. mysq基础操作

    创建表: create table customer(mid char(5) primary key,name varchar(20),birth date,sex char(1) DEFAULT ' ...

  5. Linux命令1

     1.获取当前系统支持的所有命令的列表: compgen ­-c  2.怎样查看一个linux命令的概要与用法: whatis grep #便可查到grep的用法 3.怎样一页一页地查看一个大文件的内 ...

  6. MyBatis 传List参数 nested exception is org.apache.ibatis.binding.BindingException: Parameter 'idList' not found.

    在MyBatis传入List参数时,MyBatis报错:nested exception is org.apache.ibatis.binding.BindingException: Paramete ...

  7. Linux scp sudo

    command line - scp to remote server with sudo - Super Userhttps://superuser.com/questions/138893/scp ...

  8. DAY17、常用模块

    一.time模块 1.时间戳(timestamp):time.time()     #可以作为数据的唯一标识   是相对于1970-1-1-0:0:0时间插值 2.延迟线程的运行:time.sleep ...

  9. SpringCloud学习笔记:熔断器Hystrix(5)

    1. Hystrix简介 在分布式系统中,服务与服务之间相互依赖,一种不可避免的情况是某些服务会出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞. Hystrix提供熔断器功能,能够阻止分布式 ...

  10. 通过SQL脚本来查询SQLServer 中主外键关系

    在SQLServer中主外键是什么,以及主外键如何创建,在这里就不说了,不懂的可以点击这里,这篇文章也是博客园的博友写的,我觉得总结的很好: 此篇文章主要介绍通过SQL脚本来查看Sqlserver中主 ...