LeetCode OJ--Sum Root to Leaf Numbers
https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
给一棵树,找从根到叶的路径,并把路径上的数相加,求和。
树的深度优先搜索
/**
* 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 ;
vector<int> numbers;
vector<int> digits;
deep(root,numbers,digits); int sum = ;
for(int i = ; i < numbers.size(); i++)
{
sum += numbers[i];
}
return sum;
} void deep(TreeNode *root, vector<int> &numbers, vector<int> &digits)
{
digits.push_back(root->val); if(root->left == NULL && root->right == NULL)
{
int num = ;
for(int i = ; i < digits.size(); i++)
{
num *= ;
num += digits[i];
}
numbers.push_back(num);
return;
}
if(root->left)
{
deep(root->left,numbers,digits);
digits.pop_back();
}
if(root->right)
{
deep(root->right,numbers,digits);
digits.pop_back();
}
}
};
LeetCode OJ--Sum Root to Leaf Numbers的更多相关文章
- [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 ...
- 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(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- leetcode 129. Sum Root to Leaf Numbers ----- java
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [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 ...
- 【LeetCode】Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- Java for 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 ...
- Leetcode#129 Sum Root to Leaf Numbers
原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...
- LeetCode 129. Sum Root to Leaf Numbers 动态演示
树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...
- [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...
随机推荐
- 从0到n-1中随机等概率输出m个不同的数
//假设输入的n远大于m void knuth(int n, int m) { for (int i = 0; i < n; i++) { if (rand() % (n - i)<m) ...
- Poj3061Subsequence
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- S变换
哈哈,这两天在整理时频分析的方法,大部分参考网上写的比较好的资料,浅显易懂,在这谢过各位大神了! 今天准备写下S变换,由于网上资料较少,自己尝试总结下,学的不好,望各位多多指导 由前面的文章可知,傅里 ...
- day21&22&23:线程、进程、协程
1.程序工作原理 进程的限制:每一个时刻只能有一个线程来工作.多进程的优点:同时利用多个cpu,能够同时进行多个操作.缺点:对内存消耗比较高当进程数多于cpu数量的时候会导致不能被调用,进程不是越多越 ...
- Python框架之Django学习笔记(八)
模板继承 到目前为止,我们的模板范例都只是些零星的 HTML 片段,但在实际应用中,你将用 Django 模板系统来创建整个 HTML 页面. 这就带来一个常见的 Web 开发问题: 在整个网站中,如 ...
- PostgreSQL 数组类型
PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...
- c++树及树与二叉树的转换
此算法中的树结构为“左儿子有兄弟链接结构” 在这样的一个二叉树中,一个节点的左分支是他的大儿子节点,右分支为他的大兄弟节点. 这里讲的树有递归前根,中根,后根遍历,插入节点,插入兄弟节点,查找结点,释 ...
- 理解机器为什么可以学习(三)---Theory of Generalization
前边讨论了我们介绍了成长函数和break point,现在继续讨论m是否成长很慢,是否能够取代M. 成长函数就是二分类的排列组合的数量.break point是第一个不能shatter(覆盖所有情形) ...
- python 文件(file)操作
操作文件的一般流程有: 打开文件.文件处理.关闭文件 开开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[不可读: 不存在则创建:存在 ...
- idea热部署设置(复制)
提出问题 IntelliJ IDEA工具如何设置热部署??? 解决问题 我的IDEA的版本是:IntelliJ IDEA 14.0.2 第一步:打开tomcat配置 这里写图片描述 第二步: 这里写图 ...