[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. ...
随机推荐
- 三、Linux 系统目录结构
Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最 ...
- php v8js
本文整理自大神 Corz 1.php56 /datas/soft/php56/bin/php -v PHP (cli) #https://blog.csdn.net/lzm198707/article ...
- 怎样查看web软件例如apache的连接数
查看连接总数和当前的连接数 netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l 查看IP访 ...
- FLASH、SDRAM
1.flash: 闪存,掉电之后里面的存储数据不会丢失,在嵌入式系统中用作存储Bootloader以及操作系统或者程序代码或者直接当硬盘使用(U盘).一般主要使用的FLASH有NOR flash和NA ...
- 牛课第二次多校I
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 题目描述 White Cloud has a square of n*n from (1,1) ...
- flex遭遇text-overflow:hidden,white-space:nowrap
最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...
- 【Palindrome Partitioning】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- MFC深入浅出读书笔记第二部分2
第七章 MFC骨干程序 所谓骨干程序就是指有AppWizard生成的MFC程序.如下图的层次关系是程序中常用的几个类,一定要熟记于心. 1 Document/View应用程序 CDocument存放 ...
- java面向对象之关键字,权限修饰符
1.关键字:this,static,package,importthis:1.表示对当前对象的引用!2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特 ...
- TOJ 4689: Sawtooth
4689: Sawtooth Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 26 ...