【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 ...
随机推荐
- L2-4 部落 (25 分)
在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈.我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同 ...
- SpringCloud(1)服务注册与发现Eureka
1.创建1个空白的工程 2.创建2个model工程 一个module(即SpringBoot)工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client. Eureka ...
- 三种dedecms友情链接调用标签
三种dedecms友情链接调用标签: 1.获取友情链接分类 {dede:flinktype}<span>[field:typename/]</span>{/dede:flink ...
- k8s简单的来部署一下tomcat,并测试自愈功能
前言: 2018年12月6日 今天终于把k8s运行tomcat打通了,耗了我几天时间一个一个坑踩过来,不容易啊,废话不多说. 先记录一些操作时的错误: <<<<<< ...
- Python——面向对象的特性
1.继承 定义:继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 class A:pass #父类,基类,超类 class ...
- placeholder效果
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UT ...
- 下载图片没有关闭http输入流导致下载超时
在某次接入第三方厂商数据时,需要根据对方提供的URL地址下载图片,当数据量大时会遇到很多的下载图片超时问题,开始以为是第三方厂商的问题,对方排查了很久之后,说是我这边下载数据全部留在缓存区,导致缓存区 ...
- cas单点登录-https的配置(一)
前言 由于个人的兴趣和为了加薪,在这里研究下cas单点登录,同时也记录下自己探索的过程,希望也能帮到有同样兴趣的小伙伴 环境 CAS-5.1.3 tomcat8.5 jdk8 centos6.5 ...
- [WC2018]通道——边分治+虚树+树形DP
题目链接: [WC2018]通道 题目大意:给出三棵n个节点结构不同的树,边有边权,要求找出一个点对(a,b)使三棵树上这两点的路径权值和最大,一条路径权值为路径上所有边的边权和. 我们按照部分分逐个 ...
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...