网址: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. java 软件安装

    myeclipse 2014 安装 https://www.cnblogs.com/plus301/articles/5633540.html

  2. 15.1-uC/OS-III资源管理(锁调度器)

    1.大部分独占资源的方法都是创建临界段:1) 关中断方式2) 锁调度器方式3) 信号量方式4) mutex方式 2.独占共享资源的最快和最简单方法是关中断 然而,关/开中断是和CPU相关的操作,其相关 ...

  3. GTK# on Ubuntu DllMap

    修改配置:/etc/mono/config 新增以下代码 <dllmap dll="libglib-2.0-0.dll" target="libglib-2.0.s ...

  4. html5dom2

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  5. vue全局使用axios插件请求ajax

    vue全局使用axios插件请求ajax Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方宣布停止更新vue-resource,并推 ...

  6. Node.js基础学习一之Get请求

    本人从事的是前端开发,这段时间公司开发项目比较少所以就想着学点东西,然后就想到了Node.js ,跟着菜鸟教程学了点,不过我觉得最好的学习方法是带着需求来学习. 其实和服务端打交道无非就是能有一个可以 ...

  7. Python while 循环

     while循环的使用 count = 0 while True: print("conunt:",count) conunt = conunt +1 注:while:作为循环命令 ...

  8. Linux 管理服务启动工具

    工具:ntsysv(图形,可以关闭开启服务) 安装包:ntsysv-1.3.30.2-2.el5 工具:chkconfig –list(文字,开启关闭服务) 自定义加服务:执行脚本放入:/etc/in ...

  9. UI自动化(五)dom

    获取标签 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // 直接获取标签   document.getEleme ...

  10. 【jvisualvm远程监控配置】

    第一步:首先通过hostname -i  和ifconfig命令,查看一下IP地址是否一致 如果不一致,修改hosts的IP地址:vi /etc/hosts    把ip地址和ifconfig的地址修 ...