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.
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,
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.
分析
按照二叉树的深度遍历,累计所有路径组成整数的和。
使用二叉树的递归深度优先遍历实现!
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 sumNumbers(TreeNode* root) {
int ret = 0;
if (!root)
return ret;
else if (!root->left && !root->right)
{
ret = root->val;
return ret;
}
else
dfs(root, root->val , ret);
return ret;
}
//深度优先遍历,得到所有根节点到叶子节点的路径和
void dfs(TreeNode *root, int val, int &sum)
{
if (!root->left && !root->right)
sum += val;
if (root->left)
{
dfs(root->left, val * 10 + root->left->val, sum);
}
if (root->right)
{
dfs(root->right, val * 10 + root->right->val, sum);
}
}
};
LeetCode(129) Sum Root to Leaf Numbers的更多相关文章
- LeetCode之“树”:Sum Root to Leaf Numbers
题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...
- 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】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 解题报告
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 ...
- 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 ...
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 129. Sum Root to Leaf Numbers(从根节点加到叶子节点的和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb ...
随机推荐
- kali linux 通过跑包的方式破解wifi密码
1. wlan0开启monitor mode : airmon-ng start wlan0 2. 查看附近的无线网络 : airodump-ng wlan0mon 3. 抓取无线 ...
- LinkedList源码及原理
简介 内部结构分析 LinkedList源码分析 构造方法 添加(add)方法 根据位置取数据的方法 根据对象得到索引的方法 检查链表是否包含某对象的方法: 删除(remove/pop)方法 Link ...
- BI的意义
BI系统建设的价值,有可能不值钱,也有可能价值数千万,就看我们大家好用了没.”所以,BI系统建设的收获,终究还是因企业而异的,再归根,便是与企业的文化,与企业的人,尤其是管理层是极为相关的了. 商业智 ...
- Spring Bean的一生
Spring Bean的一生 When you work directly in Java, you can do anything you like with your objects and do ...
- Mongodb聚合函数
插入 测试数据 for(var j=1;j<3;j++){ for(var i=1;i<3;i++){ var person={ Name:"jack"+i, Age: ...
- css3Transitions 实现的鼠标经过图标位移、旋转、翻转、发光、淡入淡出等多种特效
HTML如下: 1 <div class="container"> 3 <!--特效1 --> <section id="set-1&q ...
- Beginning Python Chapter 1 Notes
James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...
- SharePoint 2013 安装配置(3-1)
在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ...
- 并发教程--JAVA5中 计数信号量(Counting Semaphore)例子
并发教程--JAVA5中 计数信号量(COUNTING SEMAPHORE)例子 本文由 TonySpark 翻译自 Javarevisited.转载请参见文章末尾的要求. Java中的计数信息量(C ...
- ansible 任务委派 delegate_to
ansible 任务委派功能delegate_to run_noce: true 在一个主机上面只执行一次一个任务. ,如果没有这个参数的话,每个playbook中的组的主机都会执行一次. 我们有的 ...