[LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
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
.
这题是简单地深度搜索bfs,代码如下:
#include <iostream>
using namespace std; /**
* Definition for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
int sumNumbers(TreeNode *root) {
unsigned int cur_val=,ret_val=;
help_f(root,cur_val,ret_val);
return ret_val;
}
void help_f(TreeNode *node,unsigned int &cur_val,unsigned int &ret_val)
{
if(node==NULL) return ;
cur_val=cur_val*+node->val;
help_f(node->left,cur_val,ret_val);
help_f(node->right,cur_val,ret_val);
if(node->left==NULL&&node->right==NULL) ret_val+=cur_val;
cur_val/=;
}
}; int main()
{ return ;
}
[LeetCode] Sum Root to Leaf Numbers dfs,深度搜索的更多相关文章
- 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 (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- [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 ...
- 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 ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
随机推荐
- js浮点数加减乘除
浮点数精确计算 /** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. ** 调用:ac ...
- JZOJ 5771. 【NOIP2008模拟】遨游
5771. [NOIP2008模拟]遨游 (File IO): input:trip.in output:trip.out Time Limits: 2000 ms Memory Limits: 2 ...
- python学习——StringIO和BytesIO
StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. StringIO顾名思义就是在内存中读写str. 要把str写入StringIO,我们需要先创建一个StringIO,然后,像文 ...
- Hyper-V动态迁移中?小心性能损失
动态迁移是虚拟化技术的一个标志,它允许虚拟机在服务器间进行动态迁移.调节负载平衡.性能管理.备灾管理和数据中心维护.Windows Server 2012 R2中的Hyper-V动态迁移默认功能具备相 ...
- 55、android app借助友盟实现微信授权登录
一.去微信开放平台的管理中心申请移动设备的审核(需进行开发者资质认证,每年300元) 1.获取应用的签名 2.在微信开放平台申请移动应用 两个注意点:①签名要填对 ②应用的包名要写对(tips: co ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 三种框架对比react vue 和Angular对比
https://blog.csdn.net/runOnWay/article/details/80103880 angular 优点 背靠谷歌 使用typescript 便于后端人员开发上手 完整 不 ...
- html之表单标签
表单标签的属性: 用于向服务器传输数据 表单能够包含input元素,比如文本字段,复选框,单选框,提交按钮等等 表单还可以包含textarea(简介之类的),select(下拉),fieldset和l ...
- 深入学习之mysql(一)数据库操作
1.显示所有数据库: SHOW DATABASES; 2.创建数据库: CREATE DATABASE 数据库名: 3.选择你所创建的数据库: USE 数据库名; 4.删除数据库: DROP 数据库名 ...
- php获取客户端mac地址
exec('/sbin/arp -a 2>&1', $array, $return_val);dump($array);$mac = '';foreach($array as $valu ...