题意:

  给一棵二叉树,每个节点上有一个数字,范围是0~9,将从根到叶子的所有数字作为一个串,求所有串的和。

思路:

  普通常规的DFS。

 /**
* 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) {
if(root==NULL) return ;
return DFS(root,);
}
int DFS(TreeNode* t,int sum)
{
sum=sum*+t->val;
if(!t->left && !t->right) return sum;
int left=, right=;
if(t->left) left=DFS(t->left,sum);
if(t->right) right=DFS(t->right,sum);
return left+right;
}
};

AC代码

LeetCode Sum Root to Leaf Numbers(DFS)的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  4. 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 ...

  5. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. python——使用readline库实现tab自动补全

    Input History readline tracks the input history automatically. There are two different sets of funct ...

  2. 特征值提取之 -- TF-IDF值的简单介绍

    首先引用百度百科的话: "TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料 ...

  3. HDU 5773 The All-purpose Zero 求LIS

    求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以 ...

  4. VS2003编译后的网站如何修改代码

    VS2003编译后的网站,如果没有源代码,而要修改里面的代码时,可以以以下方式解决: 反编译dll,把找出cs代码文件,然后重新建一个类项目,把此文件中的代码修改后重新生成dll,放在编译的网站中的b ...

  5. 第四章 CSS基础

    1.CSS是cascading style sheets 层叠样式表.样式定义如何显示html元素,通常存储在样式表中,将样式添加到html中,是为了解决内容与表现分离的问题. 2.外部样式表可以极大 ...

  6. IBatis.Net 表连接查询(五)

    IBatis.Net之多表查询: 一.定制实际对应类的方式 首先配置多表的测试数据库,IBatis.Net之Oracle表连接查询配置: 首先新建两张表如下: 为两张表建立外键: ALTER TABL ...

  7. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast

    Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...

  8. Great writers inspire

    William Shakespeare Beowulf (Anglo-Saxon) Jonathan Swift Stephen Duck William Blake George Elliot Ka ...

  9. 【温故知新C/C++/opencv】取址符&||cv::groupRectangles||引用与值传递

    cv::groupRectangles void groupRectangles(vector<Rect>& rectList, int groupThreshold, doubl ...

  10. ios 8+ (xcode 6.0 +)应用程序Ad Hoc 发布前多设备测试流程详解

    我们开发的程序在经过simulator以及自己的iOS设备测试后,也基本完成应用程序了,这时候我们就可以把它发布出去了更更多的人去测试,我们可以在iOS平台使用ad hoc实现. 你在苹果购买的开发者 ...