23. Sum Root to Leaf Numbers
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.
For example,
1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
思想: 分三种情况: 1. 当前结点为空,返回0. 2. 叶子结点, 返回当前值. 3. 父结点,返回左右两个 path 值的和。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int dfs(TreeNode *root, int sum) {
if(root == 0) return 0;
sum = sum * 10 + root->val;
if(root->left || root->right)
return dfs(root->left, sum) + dfs(root->right, sum);
return sum;
}
class Solution {
public:
int sumNumbers(TreeNode *root) {
return dfs(root, 0);
}
};
23. Sum Root to Leaf Numbers的更多相关文章
- LeetCode: Sum Root to Leaf Numbers 解题报告
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 (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- 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之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-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] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [Swift]LeetCode129. 求根到叶子节点数字之和 | 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 129. Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 129. Sum Root to Leaf Numbers pathsum路径求和
[抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
随机推荐
- Linux gcc 编译日记
gcc 编译器是众多编译器组合入口,例如在编译 .cpp 文件时,使用c++ 编译器,编译.c 文件时,使用c编译器. 在编译c++程序时, 库文件与头文件可通过 -L[dir] 指定库目录 , -l ...
- linux命令每日一练习-top free
top看看进程的内存使用情况 free产看内存使用情况 top -n 2 -b > log.txt 将更新两次的结果输入到log.txt cat > log.txt //清空文件并写入 ...
- BaiduMap Search List
using AnfleCrawler.Common; using HtmlClient; using System; using System.Collections.Generic; using S ...
- 《Java中的不可变类》
//不可变类举例: /* 下面程序试图定义一个不可变类Person类,但=因为Person类包含一个引用类型的成员变量, 且这个引用类是可变类,所以导致Person类也变成了可变类. */ class ...
- android:LayoutInflater
LayoutInflater:一般用于查找res/layout下的布局文件,findViewById()一般是用于查找布局下的各种控件 一般:我们使用LayoutInflater.from(conte ...
- bsp STEP
Web开发不仅现在比较流行,将来也会.我来谈一下最近bsp application项目的体会吧,属初学者,请各位多多指教. SAP 的web开发方法有很多种,bsp只是其中一种,而bsp开发有可以分 ...
- CSS行内元素和块级元素的居中
一.水平居中 行内元素和块级元素不同,对于行内元素,只需在父元素中设置text-align=center即可; 对于块级元素有以下几种居中方式: 1.将元素放置在table中,再将table的marg ...
- Android 学习第6课,循环功能
package ch02; public class jiujiuchengfa { public static void main(String[] args) { // TODO 自动生成的方法存 ...
- 细说Javascript之null、undefined和NaN
首先简单介绍一下Javascript中的数据类型,Javascript中的数据类型有undefined,boolen,number,string和object等5种,前4种是原始类型,第5种是引用类型 ...
- CentOS 7 编译安装 Code::Blocks
CentOS 7 编译安装 Code::Blocks yum install cairo-devel yum install pango-devel yum install atk-devel yum ...