1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址: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从根到叶的二进制数之和的更多相关文章
- 【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 ...
- 【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 ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- Leetcode 1022. Sum of Root To Leaf Binary Numbers
dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...
- 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 ...
- [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 ...
- LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)
这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...
- [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 ...
- [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 ...
随机推荐
- 接口测试工具-Jmeter使用笔记(六:从文本读取参数)
使用场景:测试一个接口并发处理数据的能力,并且每次请求传入的参数都要不同. 解决方法--- CSV Data Set Config 列举一个实例,步骤中会侧重读取参数操作的说明,其他有疑问的步骤请查阅 ...
- 检测到包降级: Microsoft.Extensions.Configuration.Abstractions 从 2.1.1 降 2.1.0
解决方法:工具-nuget管理包-程序管理控制台-选择 项目- 执行 -Install-Package Microsoft.Extensions.Configuration.Abstractions ...
- 设置mac笔记本为固定ip
第一步.点击Mac桌面“系统偏好设置”图标 第二步.在打开的系统偏好设置界面,点击互联网和无线选项中的“网络” 第三步.在网络界面,点击“高级”,进入高级设置. 第四步.在以太网设置界面,在TCP/ ...
- JAVA微信支付代码(WeChatPay.java 才是调用类)
微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/index.html MD5Util.java package weixin; import java.se ...
- 笔记:Linux(AWS Redhat)开机启动workman进程(/etc/rc.local必须是755权限)
1.使用which命令查看php的安装路径 [root@ip---- ~]# which php /usr/bin/php 2.使用vim /etc/rc.local将要执行的命令加入/etc/rc. ...
- C 二叉查找树的基本操作
最近研究一下二叉树排序问题,找到的资料还真是五花八门,说得也是千奇百怪. 分析一下原因,还是因为数的特性,造成结果的不唯一性造成的大家看了很多,似乎都有理,好像明白了,一综合又糊涂了的结果. 我这里给 ...
- CentOS 7 源码编译MariaDB
下载源码包 安装 SCL devtoolset-7 SCL(Software Collections)可以让你在同一个操作系统上安装和使用多个版本的软件,而不会影响整个系统的安装包.SCL为社区的以 ...
- 浅谈react的初步试用
现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. React 起源于 Face ...
- python 装饰器(语法糖)
def login(func): def testlogin(): for i in range(3): _username="abc" ...
- 【js】深拷贝一文中的几个错误点
原文:https://www.cnblogs.com/wuhairui/p/10370227.html 得到网友反馈,试过后也再查了下资料: 1.JSON.parse(JSON.stringify(o ...