sum-root-to-leaf-numbers——dfs
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.
/**
* 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) {
if(root==NULL)
return ;
res=;
dfs(root,);
return res;
}
void dfs(TreeNode *root,int num){
if(root!=NULL){
num=num*+root->val;
}
if(root->left==NULL&&root->right==NULL){
res+=num;
}
if(root->left!=NULL){
dfs(root->left,num);
}
if(root->right!=NULL){
dfs(root->right,num);
}
}
int res;
};
sum-root-to-leaf-numbers——dfs的更多相关文章
- 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 dfs,深度搜索
		
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
 - Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
		
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
 - 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  解题报告
		
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】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 ...
 - 129. 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 [tree、dfs]
		
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
 
随机推荐
- C++ 将string转换成char*字符串
			
我们经常会使用C和C++的混合编程,在某些情况下,需要将C++的string,转换成char* 的字符串.下面说两种可行的方法,作为总结. 1. data(); 如: string str=" ...
 - maya 2014帮助手册中   三维概念讲解
			
maya 2014 帮助手册中 三维概念讲解 多边形简介 三个或更多的边, 顶点 边 面 组成 经常使用三边形或四边形来建模 n边形不常用 单个多边形称为面 多个面连接到 ...
 - 关于css的float
			
什么是浮动? CSS中的一些元素是块级元素,表示它们会自动另起一行. 举个例子,如果你创建了两个段落,每个段落都只有一个单词.这两个单词不会靠在一起,而是会各自占据一行. 另一些元素是行内元素,表示它 ...
 - POJ #1025 Department
			
模拟题. 这题第一个障碍是现在少见的循环电梯 ('pater-noster' elevator) "The building has `pater-noster' elevator, i.e ...
 - 单线程实现并发——协程,gevent模块
			
一 并发的本质 1 切换 2 保存状态 二 协程的概念 协程,又称微线程,纤程.英文名Coroutine.单线程下实现并发,用户从应用程序级别控制单线程下任务的切换,注意一定是遇到I/O才切. 协程的 ...
 - bzoj 3208 花神的秒题计划I
			
bzoj 3208 花神的秒题计划I Description 背景[backboard]: Memphis等一群蒟蒻出题中,花神凑过来秒题-- 描述[discribe]: 花花山峰峦起伏,峰顶常年被雪 ...
 - 以iphone6plus 为标准单位是px的页面 在运行时转换为rem
			
在页面中引入以下代码,把样式中带px单位的样式放到本页面中的<style>标签中 /** * Created by Administrator on 2017-03-14. */ /*** ...
 - [转]iOS7 后台执行
			
[转自:http://esoftmobile.com/2013/06/23/ios7%E7%A8%8B%E5%BA%8F%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C/] i ...
 - LeetCode OJ--Construct Binary Tree from Preorder and Inorder Traversal  *
			
http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 根据二叉树的前序遍 ...
 - Xamarin XAML语言教程XAML文件结构与解析XAML
			
Xamarin XAML语言教程XAML文件结构与解析XAML XAML文件结构 在上文中,我们创建XAML文件后,会看到类似图1.16所示的结构 图1.16 结构 其中,.xaml文件和.xaml ...