网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/

递归调用求和,同时注意%1000000007的位置

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

1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和的更多相关文章

  1. 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers

    problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...

  2. 【leetcode】1022. Sum of Root To Leaf Binary Numbers

    题目如下: Given a binary tree, each node has value 0 or 1.  Each root-to-leaf path represents a binary n ...

  3. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  4. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

  5. LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)

    1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...

  6. [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers

    Given a binary tree, each node has value 0 or 1.  Each root-to-leaf path represents a binary number ...

  7. LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)

    这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...

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

随机推荐

  1. 报错解决——xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    一般在遇到这个问题的时候都是想用git或者svn,结果发现用不了并报错xcrun: error: invalid active developer path (/Library/Developer/C ...

  2. 经验分享 | 如何拿到自己满意的offer?

    本文阅读时间约16分钟 最近两年,人工智能(AI)就像一个点石成金的神器,所有的行业,创业公司,或是求职,只要沾着这个词,多少有点脚踩五彩祥云的感觉,故事来了,融资来了,高薪来了. 于是,越来越多的人 ...

  3. Tomcat出现The origin server did not find a current representation for the target resourc...

    访问页面出现404 解决方法: https://blog.csdn.net/dbc_121/article/details/79204340 我的问题主要还是在tomcat调整上, 对了,关于loca ...

  4. 微信小程序组件封装

    第一步,在page下面新建一个template文件,如下图 第二部,在template.wxml中编写公用组件即要封装的代码模块 <!--pages/template/template.wxml ...

  5. Mysql数据库优化之SQL及索引优化

    1. 如何发现有问题的SQL?  使用mysql慢查询日志对有效率问题的Sql进行监视 (1) show  variables like 'slow_query_log';     查看慢查询日志是否 ...

  6. Java 实现异步调用

    首先 我遇到的问题是 接口调用时需要更新缓存 而更新缓存又是个说快不快的过程 所以打算做异步调用 返回我所需要的结果即可 ,至于缓存什么时候更新完 就不是我所需要关注的了 废话不多说 上代码 publ ...

  7. lmbench用于arm测试

    一.下载 http://www.bitmover.com/lmbench/lmbench.html网站有lmbench的介绍 下载地址如下(lmbench3): http://www.bitmover ...

  8. Dart基础-泛型和库

    https://blog.csdn.net/hekaiyou/article/details/46774727

  9. Centos安装Python各版本解释器并配置pip

    Centos7.3安装Python3.7 Python3.7貌似又多了新的依赖,所以按照安装之前的套路安装在配置pip阶段就会出问题,比如: ModuleNotFoundError: No modul ...

  10. JavaScript Dom 绑定事件

    JavaScript  Dom 绑定事件 // 先获取Dom对象,然后进行绑定 document.getElementById('xx').onclick document.getElementByI ...