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 value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
Return the sum of these numbers.
LeetCode1022. Sum of Root To Leaf Binary Numbers
Example 1:

Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Note:
2. node.val is 0 or 1.
3. The answer will not exceed 2^31 - 1.
Java 实现
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public int sumRootToLeaf(TreeNode root) {
return def(root, 0);
}
public int def(TreeNode root, int sum) {
if (root == null) {
return 0;
}
sum = 2 * sum + root.val;
if (root.left == null && root.right == null) {
return sum;
}
int leftSum = root.left == null ? 0 : def(root.left, sum);
int rightSum = root.right == null ? 0 : def(root.right, sum);
return leftSum + rightSum;
}
}
参考资料
- https://leetcode-cn.com/problems/sum-of-root-to-leaf-binary-numbers/
- https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)的更多相关文章
- [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_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 ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- 【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
题目如下: Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary n ...
- LeetCode1022. 从根到叶的二进制数之和
题目 class Solution { public: int ans = 0; int sumRootToLeaf(TreeNode* root) { dfs(root,0); return ans ...
- [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 ...
随机推荐
- driud 异常
异常如下: 十二月 25, 2017 11:37:14 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetProper ...
- MySQL:服务无法启动(1067)问题
打开安装文件下的my.ini 找到: #Path to the database rootdatadir="C:/ProgramData/MySQL/MySQL Server 5.5/dat ...
- nodejs express cheerio request爬虫
const express = require('express') const cheerio = require('cheerio') const request = require(" ...
- python技巧获取26个英语字母
import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...
- distribution system index
Resiliency:可译为容错性,强调从错误状态恢复的能力.形容词Resilient可译作“可容错的”. Elasticity:可译为伸缩性,强调根据负载进行水平伸缩的能力.形容词Elastic可译 ...
- (转载)RNA表观遗传学开创者何川
何川,RNA表观遗传学开创者.早年毕业于中国科技大学,2000年获麻省理工学院博士学位,2000到2002年在哈佛大学做博士后研究,2002年至今执教芝加哥大学化学系, 是芝加哥大学生物物理动态研究所 ...
- vue子组件数据变化同步到父组件中
方法:通过watch监听子组件数据变化 1.父组件中注册方法 <Child @getChildValue="getChildValue"></Child> ...
- Chaos Engineering 混沌工程 Chaos Monkey vs Chaos xxx vs Chaos Blade
Chaos Engineering的历史.原则以及实践https://www.infoq.cn/article/chaos-engineering-the-history-principles-and ...
- 卸载node和npm
sudo npm uninstall npm -g yum remove nodejs npm -y
- postgre alter命令修改字段
参考文档:https://www.yiibai.com/postgresql/postgresql_alter_command.html PostgreSQL ALTER TABLE命令用于添加,删除 ...