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== ...
随机推荐
- 模块之 logging模块 time 模块 random模块 sys模块 pickle模块
1.如果执行文件不在项目根目录下,需要添加项目根目录到sys.path中2.调用业务逻辑 2.logging模块 程序日志是 什么时间发生了什么事情,以及当时的情况 不是logging的话 记录日志的 ...
- python3 练习题100例 (七)
题目七:将一个列表的数据复制到另一个列表中. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目七:将一个列表的数 ...
- HBase(0.94.5)的Compact和Split源码分析
经过对比,0.94.5以后版本主要过程基本类似(有些新功能和细节增加) 一. Compact 2.1. Compact主要来源 来自四个方面:1.Memstoreflush时:2.HR ...
- 修改python新建文件时的模板
- 校内考试之zay与银临(day1)
T1大美江湖(洛谷P5006) zayの题解: 这个题的本质是模拟 不过有卡ceil的地方 ceil是对一个double进行向上取整,而对于int/int来说,返回值是int 举个生动的栗子 ceil ...
- Java面向对象---方法的创建与重载
方法的创建 方法就是可重复调用的代码段. 定义: 访问修饰符 返回值类型 方法名(参数){ 方法主体 } 返回值类型:void(无返回值):基本数据类型:应用数据类型:类对象等. 方法名的命名规则:第 ...
- GBDT算法简述
提升决策树GBDT 梯度提升决策树算法是近年来被提及较多的一个算法,这主要得益于其算法的性能,以及该算法在各类数据挖掘以及机器学习比赛中的卓越表现,有很多人对GBDT算法进行了开源代码的开发,比较火的 ...
- JVM内存管理:深入Java内存区域与OOM
Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想进去,墙里面的人却想出来. 概述: 对于从事C.C++程序开发的开发人员来说,在内存管理领域,他们即是拥有最高权力的皇帝 ...
- axure rp教程(四)动态面板滑动效果
转载自: http://www.iaxure.com/74.html 实现目标: 1. 点击登录滑出登录面板 2. 点击确定滑出动态面板 最终效果如下: 这种效果可以通过两种方法实现: 首先准备需 ...
- 50、转自知乎上android开发相见恨晚的接口
原文链接:http://www.zhihu.com/question/33636939 程序员软件开发Android 开发JavaAndroid修改 Android开发中,有哪些让你觉得相 ...