LeetCode题解之 Sum of Left Leaves
1、题目描述

2、问题分析
对于每个节点,如果其左子节点是叶子,则加上它的值,如果不是,递归,再对右子节点递归即可。
3、代码
int sumOfLeftLeaves(TreeNode* root) {
if (root == NULL)
return ;
int ans = ;
if (root->left != NULL) {
if (root->left->left == NULL && root->left->right == NULL)
ans += root->left->val;
else
ans += sumOfLeftLeaves(root->left);
}
ans += sumOfLeftLeaves(root->right);
return ans;
}
LeetCode题解之 Sum of Left Leaves的更多相关文章
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- [LeetCode 题解] Combination Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a se ...
- [LeetCode 题解]: Two Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- LeetCode题解——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...
- LeetCode算法题-Sum of Left Leaves(Java实现)
这是悦乐书的第217次更新,第230篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第85题(顺位题号是404).找到给定二叉树中所有左叶的总和.例如: 二叉树中有两个左叶 ...
- LeetCode之404. Sum of Left Leaves
------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...
- LeetCode题解之Sum Root to Leaf Numbers
1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...
- 【leetcode❤python】 Sum of Left Leaves
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
随机推荐
- freemark null处理
以下引用官方描述: 引用The FreeMarker template language doesn't know the Java language null at all. It doesn't ...
- Linux-(rcp,scp)
rcp命令 1.命令格式: rcp [参数] [源文件] [目标文件] 2.命令功能: rcp命令用在远端复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前 ...
- map集合的见解、排序
map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等 HashMap:我们最常用的Map,它根据key的HashCode 值 ...
- java 分库关联查询工具类
问题: 由于公司业务扩大,各个子系统陆续迁移和部署在不同的数据源上,这样方便扩容,但是因此引出了一些问题. 举个例子:在查询"订单"(位于订单子系统)列表时,同时需要查询出所关联的 ...
- How to check Logstash's pulse
Have you ever wondered if Logstash was sending data to your outputs? There's a brand new way to chec ...
- mongorestore 一次踩雷
1.在做mongodb备份后,研发突然有个需求说先看一下昨天备份里面的数据,进行一下核实.因为那部分数据今天已经删除,由于使用---gzip.--archive做的备份,所以必须导入到同名的数据库里面 ...
- mysql密码篇(一)
1.mysql密码样例.用于密码重置: password: e9297341f5073b9e557239592f4540ba690538058e7761822372942119992d0fsalt: ...
- 疯狂Java讲义PDF
java学习资料,仅供学习交流,自行取用↓ 链接:https://pan.baidu.com/s/1dF1wCST 密码:i75g
- WCF 学习总结1 -- 简单实例
从VS2005推出WCF以来,WCF逐步取代了Remoting, WebService成为.NET上分布式程序的主要技术.WCF统一的模型整合了以往的 WebService.Remoting.MSMQ ...
- Windows下当地RabbitMQ服务的安装
Windows下本地RabbitMQ服务的安装 本文参考:刘若泽相关技术文档 当然这些内容页可以通过RabbitMQ官方网站获得. RabbitMQ配置说明手册 一.RaibbitMQ服务器配置 1. ...