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 ...
随机推荐
- C# 实现CRC16校验
前言 本文将使用一个NuGet公开的组件技术来实现CRC16校验功能,提供了一些简单的API,来方便的实现. 在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控 ...
- php之强制回调类型callable
<?php function demo(callable $fn) { $fn(); } function callback() { echo __FUNCTION__,'<br/> ...
- poj 3694 无向图求桥+lca
题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...
- 框架tensorflow1
TensorFlow 1 分类: 1,protocol Buffer 处理结构化数据工具: (xml,json) 2,Bazel 自动化构建工具, 编译: tensor 张量: ...
- XXS level7
(1)输入与第六关相同的Payload:"><A HREF="javascript:alert()"> 查看页面源代码,发现“herf"被过滤 ...
- Java实现带logo的二维码
Java实现带logo的二维码 二维码应用到生活的各个方面,会用代码实现二维码,我想一定是一项加分的技能.好了,我们来一起实现一下吧. 我们实现的二维码是基于QR Code的标准的,QR Code是由 ...
- pycharm 在线激活
1.在pycharm过期页面选择 enter License 2.激活界面的License server输入:http://idea.liyang.io 然后点击激活 (确保电脑能上网,亲测有效)
- WebSocket是什么原理?为什么可以实现持久连接?
作者:Ovear 链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- babelrc 中的 presets 字段(env, react)和 plugins 字段(dynamic-import-webpack, transform-object-rest-spread, ...)
一.presets 字段 目前用到 presets: [ 'env', 'react' // react 转码规则 ]: 只有 env 时,作用和 latest 相同,包括 es5.es6.es7 ...
- Delphi在调WebService的时候加Soap头验证
procedure ws: WebServiceSoap; H: XXXHeader; begin ws := GetWebServiceSoap; H := XXXHeader.Cr ...