LeetCode 404. Sum of Left Leaves (C++)
题目:
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
分析:
给定一颗二叉树,求左叶子节点的和。
重点在于如何判断左叶子节点,如果一个节点的left存在,且left的left和right都为空,那么我们就可以将这个节点的left->val记录下来。递归处理整颗树即可。
程序:
/**
* 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)
return ;
int sum = ;
if (root->left && !root->left->right && !root->left->left){
sum = root->left->val;
}
return sum + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
LeetCode 404. Sum of Left Leaves (C++)的更多相关文章
- LeetCode 404. Sum of Left Leaves (左子叶之和)
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- LeetCode - 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...
- [leetcode]404. Sum of Left Leaves左叶子之和
弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...
- 【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&Python] Problem 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- LeetCode之404. Sum of Left Leaves
------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
随机推荐
- 在文章详情页调用seo标题标签
在新闻的详情页,调用当前栏目的seo标题,原标签是:{dede:field.seotitle /} {dede:type} [field:seotitle/] {/dede:type} 修改 \inc ...
- nagios-4.0.8 安装部署
1.Nagios工作原理 Nagios周期性调用插件检测服务器状态,并维持一个队列,所有插件返回状态信息都进入队列,Nagios每次从队首开始读取信息,并把状态通过web显示. 安装完成后,在nagi ...
- C#做一个简单的进行串口通信的上位机
C#做一个简单的进行串口通信的上位机 1.上位机与下位机 上位机相当于一个软件系统,可以用于接收数据.控制数据.即可以对接收到的数据直接发送操控命令来操作数据.上位机可以接收下位机的信号.下位机是 ...
- Linux文件系统测试工具
一.文件系统测试工具简介 1.LTP 参考网站:http://oss.sgi.com/projects/ltp/ LTP(Linux Test Project)是由SGI和IBM联合发起的项目,提供一 ...
- ArrayProxy-Emberjs
ember 2.18版本API翻译之Ember.ArrayProxy import ArrayProxy from '@ember/array/proxy'; ArrayProxy(数组代理)包装实现 ...
- Linux基础入门 第二章 Linux终端和shell
Linux终端 进入编辑IP地址命令:vi /etc/sysconfig/network-scripts/ifcfg-eth0 按键“i”:进行编辑 按键“ESC”:退出编辑 按键“:”:输入wq, ...
- layui水平导航条三级
需求 需要做一个顶部的水平导航条,有三级,展开的时候二级和三级一起展开,结果如图: 效果 一级菜单 二级标题 三级菜单 三级菜单 二级标题 三级菜单 三级菜单 一级菜单 二级标题 三级菜单 ...
- PI接口开发之调java WS接口(转)
java提供的WSDL:http://XXX.XXX.XXX.XX/XXXXXXXcrm/ws/financialStatementsService?wsdl 登陆PI,下载Enterprise Se ...
- linux ssh 连接设置
! 本文编辑中 centos ssh 无法连接
- c++ switch语句
一.认识switch格式 switch(表达式) { case 常量表达式: 语句1; break; case 常量表达式: 语句2; break; case 常量表达式: 语句3; break; . ...