LeetCode OJ 129. Sum Root to Leaf Numbers
题目
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
Input: [1,2,3]
1
/ \
2 3
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.
Example 2:
Input: [4,9,0,5,1]
4
/ \
9 0
/ \
5 1
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.
解答
今天又选了一道水题,就是二叉树的先序遍历,又是一遍AC。。。感觉这种题目都不用动脑子,明天选一道要动脑子的题目。
下面是AC的代码:
/**
* 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 sum = 0;
int temp = 0;
void preorder(TreeNode *node){
if(node == NULL){
return ;
}
temp = temp * 10 + node->val;
if(node->left == NULL && node->right == NULL){
sum += temp;
}
else{
preorder(node->left);
preorder(node->right);
}
temp = (temp - node->val) / 10;
return ;
}
int sumNumbers(TreeNode* root) {
preorder(root);
return sum;
}
};
138
LeetCode OJ 129. Sum Root to Leaf Numbers的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- 【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- leetcode 129. Sum Root to Leaf Numbers ----- java
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- Spring中的@Bean注解、@Configuration注解、@Value
1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...
- iOS 弹幕制作
离职的最后一天,在公司学习下弹幕的制作.基于OC. 主要思路: 1.首先建一个弹幕类BulletView,基于UIView,然后在该类上写个UIlabel,用于放置弹幕文字,然后前端放置一个UIIma ...
- 关于LaTeX公式排版
[转载请注明出处]http://www.cnblogs.com/mashiqi 2017/10/05 1.居中括号框住多行公式 \begin{equation*} \left\{\begin{alig ...
- Spring Boot配置文件详解
挖个坑先 http://www.cnblogs.com/itdragon/p/8686554.html http://www.cnblogs.com/jtlgb/p/8532280.html
- 条件分支语句(SWICH语句)
语法 swich(条件表达式){ Case 表达式: 语句……. Break; Case 表达式: 语句……. Break; Case 表达式: 语句……. Break; default: 语句……. ...
- python3 基础整理
基础语法 1.python中区分大小写 2.查看关键字用 import keyword print (keyword.kwlist) 3.注释 # 单行注释,多行注释的快捷键是ctr+/,取消注释的 ...
- CCF-模板生成系统-201509-3
主要是string---STL的运用 趁机整理一下erase, find, substr, replace, insert #include <bits/stdc++.h> using n ...
- ubuntu 16.04 安装 网易云
现在网易云官网上下载对应版本 文件名:netease-cloud-music_1.0.0-2_amd64_ubuntu16.04.deb 进入下载目录: 正常安装会出现错误 解决的办法是换源 换源教程 ...
- mysql命令行使用
连接数据库 mysql -P 端口号 -h 远程机地址/ip -u 用户名 -p mysql -uroot -p123456 修改数据库密码 mysqladmin -uroot -p123456 ...
- some working learning总结学习
1. Python通过pypyodbc访问Access数据库 https://blog.csdn.net/jisuanjiguoba/article/details/73163721 2. java大 ...