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 ...
随机推荐
- pve 导入 ova
匆忙记录 Proxmox includes qm importdisk as command. Extract your ova: tar -xvf *.ova Create a new VM wit ...
- PostgreSQL 常用语句
postgres=# create database mydb; CREATE DATABASE postgres=# alter database mydb; ALTER DATABASE post ...
- SpringMVC返回结果值处理器之返回一个值
处理器一共分为两类: 一类是返回到一个页面,凡是返回页面的处理器,一定会刷新页面,对应的请求应该是同步请求. 一类是返回结果,凡是返回结果的处理器,一定是请求方需要得到这个结果值,此时就需要一个有回调 ...
- (一)Sql学习之sql语言的组成
SQL语言是具有强大查询功能的数据库结构化语言.由以下几部分组成: 1.数据定义类SQL(DDL--DATE DEFINITION LANGUAGE) CREATE-创建数据库及其对象(表,索引,视图 ...
- 类别不平衡问题之SMOTE算法(Python imblearn极简实现)
类别不平衡问题类别不平衡问题,顾名思义,即数据集中存在某一类样本,其数量远多于或远少于其他类样本,从而导致一些机器学习模型失效的问题.例如逻辑回归即不适合处理类别不平衡问题,例如逻辑回归在欺诈检测问题 ...
- Enhancer | 增强子 专题
要做就做深做精! Everything needs good justification. The interpretation should be biologically and statisti ...
- Tensorflow 2 模型默认保存路径
Tensorflow 2 模型默认保存路径 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 保存: import datetime now=da ...
- TCP为什么会出现 RST
就目前遇到的情况而言,都是负载设备,或健康检查设备发送的. 为什么会出现 RST 因为具有周期性,我大概猜到了,是 lvs 对我的后端服务的健康检查导致的,联系了网络运营服务客服人员,我把.pcap给 ...
- Vue 事件结合双向数据绑定实现todolist 待办事项 已经完成 和进行中
<template> <div id="app"> <input type="text" v-model='todo' @keyd ...
- Python3基础 函数 参数为list可变类型时,使用append会影响到外部实参
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...