Leetcode 1022. Sum of Root To Leaf Binary Numbers (树遍历题)

LeetCode --- 1022. Sum of Root To Leaf Binary Numbers 解题思路 You are given therootof a binary tree where each node has a value0or1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is0 -> 1 -> 1 -> 0 -> 1, then this could represent... 阅读详情
  1. Sum of Root To Leaf Binary Numbers
    Easy
    3.3K
    183
    Companies
    You are given the root of a binary tree where each node has a 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.

The test cases are generated so that the answer fits in a 32-bits integer.

Example 1:
Input: root = [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Example 2:

Input: root = [0]
Output: 0

Constraints:

The number of nodes in the tree is in the range [1, 1000].
Node.val is 0 or 1.

解法1:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumRootToLeaf(TreeNode* root) {
        helper(root, 0);
        return sum;
    }
private:
    int sum = 0;
    void helper(TreeNode *root, int path) {
        if (!root) return;
        path = (path << 1) + root->val;
        if (!root->left && !root->right) {
            sum += path;
            return;
        }
        helper(root->left, path);
        helper(root->right, path);
        return;
    }
};
【大二组培训】2017年电子设计大赛H题总结(软件部分) 文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 前言 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码如下(示例): import numpy as np import pandas as pd import matplotlib.pyplot as plt im 阅读详情

相关推荐

机器视觉检测到底怎么做,经验分享

笔者做机器视觉检测已经有五六年了,也算是对机器视觉的检测技术有了一定的掌握。总的来说,基于机器视觉的检测技术主要包含三个部分:视觉采集模块、后台分析模块和动作决策模块。这几个分类,跟教科书上的有较大差异,很多同学觉得我在杜撰。笔者也不作反驳,因为这是笔者这些年工作经验的总结,自然区别于教科书。下面我们分开来讲这几个模块,同时会把笔者之前的论文做个推荐总结。 所有文章均可在博主的主页中找到,若无法找到,可以用搜索工具。博主的主页: https://blog.csdn.net/Aoulun 第..

Aoulun的博客 8675

1022. Sum of Root To Leaf Binary Numbers*

1022. Sum of Root To Leaf Binary Numbers* https://leetcode.com/problems/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 b...

珍妮的选择的博客 322

简化神经元模型6 -- Hindmarsh-Rose Model

简化神经元模型 6 Hindmarsh-Rose Model

weixin_73557167的博客 1425

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

virgilshi的博客 300

从原理到接法全面了解三相电

从原理到接法全面了解三相电

viixin的博客 1万+

测试dali协议的软件,DALI2.0和DALI1.0完美兼容同一个模块,测试每项可过协议

DALI2.0和DALI1.0完美兼容同一个模块,测试每项可过协议深圳聚积智能 夏先生 13246760327 QQ 349098308■IEC62386-102(V2.0)通过测试内容IEC62386-102/2.1/Test preambleIEC62386-102/3.1/Polarity testIEC62386-102/3.2/Maximum and minimum system v...

weixin_35343788的博客 1349

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

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法DFS日期 题目地址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 题目描述 Given a binary tree, each node has value 0 or 1...

负雪明烛 1707

告别偏色!手把手教你用i1Profiler 3.5为喷墨打印机制作专属ICC曲线(附D50/D65光源选择指南)

本文详细介绍了如何使用i1Profiler 3.5为喷墨打印机制作专属ICC曲线,解决设备偏色问题。从色彩管理基础、软件配置到色块打印与测量实战技巧,逐步指导用户完成曲线制作,并提供了D50/D65光源选择指南,确保色彩还原精准。适合摄影师和设计师提升打印质量。

weixin_30613343的博客 1万+

[LeetCode] 1022. Sum of Root To Leaf Binary Numbers

原题链接

Ber03的博客 438

可信数据空间:可信空间、区域功能节点、业务功能节点、连接器的关联关系

可信数据空间通过分层架构实现安全数据共享:可信空间作为顶层制定全局规则;区域功能节点协调辖区内功能节点协作;功能节点提供具体数据服务;连接器作为唯一通信通道确保安全交互。系统采用"规则自上而下约束,数据自下而上流动"机制,通过分层治理和标准化连接,在保证数据主权的同时实现跨域协作,具有策略分级执行、去中心化治理等特性,支持灵活扩展业务场景。

友莘居士的博客 1683

从0到1:OpenClaw 助力单元测试代码自动化与覆盖率飞升

OpenClaw 的横空出世,宛如一道曙光,照亮了这片黑暗的领域。它作为一款强大的本地 AI 助手,拥有着令人惊叹的能力。OpenClaw 能够深入理解代码的逻辑结构,自动分析代码中的各种功能和潜在的风险点,从而精准地生成高质量的单元测试代码。它就像是一位不知疲倦的测试专家,能够快速、准确地完成那些繁琐的测试编写工作,将开发者从重复劳动中解放出来,让他们能够将更多的精力投入到更具创造性和价值的工作中。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 476

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

1022.Sum of Root To Leaf Binary Numbers(从根到叶的二进制数之和)DescriptionDifficulty: easyExample 1:Note:分析参考代码 Description Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a b...

redtongue的博客 408

YOLO11涨点优化:卷积魔改 | DCNv2升级版本,助力检测

在DCN的基础上,增加了2个创新点,分别是调制模块和使用多个调制后的DCN模块,从形成了DCN的升级版本——DCNv2

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 967

LeetCode-Sum Root to Leaf Numbers一种算法

LeetCode-Sum Root to Leaf Numbers一种算法 题目链接:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/ Given a binary tree containing digits from 0-9 only, each root-to-leaf path could repre...

qq_30985149的博客 277

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode //C - 1022. Sum of Root To Leaf Binary Numbers

This problem asks you to calculate the sum of the binary representations of all paths from the root to a leaf in a binary tree. Each node has a value of 0 or 1, and each path from the root to a leaf forms a binary number with the most significant bit first

Made in Code 394

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode #1022. Sum of Root To Leaf Binary Numbers

题目描述: Given a binary tree, each node has value0or1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is0 -> 1 -> 1 -> ...

LawFile的博客 227

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

题目如下: Given a binary tree, each node has value0or1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is0 -> 1 -> 1 ...

weixin_34290000的博客 113

[leetcode] 1022. Sum of Root To Leaf Binary Numbers

Description You are given the root of a binary tree where each node has a 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 cou

Learning from the mistakes 203

leetcode 1022. Sum of Root To Leaf Binary Numbers(python)

描述 You are given the root of a binary tree where each node has a 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 repres

王大呀呀的博客 323

【Leetcode】1022. Sum of Root To Leaf Binary Numbers

题目地址: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 给定一棵二叉树,只含000和111,求所有从树根到叶子的路径上的数字排列起来得到的数(这个数要视为二进制)之和。 思路是DFS。遍历到一个节点的时候,要把从树根到当前节点得到的数作为参数传递下去。得到上面传来的数xxx的时候,当前节点是数如果是yyy,则从树根到当前节点组成的数就是2x+y2x+y2x+y。如果当前节点是叶子,则累加和,否则继续递归左右子树。代码如下

数学、算法爱好者的博客 173

LeetCode(Tree)1022. Sum of Root To Leaf Binary Numbers

LeetCode(Tree)1022. Sum of Root To Leaf Binary Numbers

weixin_45428910的博客 272

【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/ 题目描述: Given a binary tree containing digits fro...

负雪明烛 1741

Golang Leetcode 1022. Sum of Root To Leaf Binary Numbers.go

思路 每次遍历的时候,之前的路径*2 + 当前的节点值当做路径表示的整数。 code type TreeNode struct { Val int Left *TreeNode Right *TreeNode } var ret int func sumRootToLeaf(root *TreeNode) int { ret = 0 helper(root, 0) retur...

anakinsun的博客 221

leetcode 1022. Sum of Root To Leaf Binary Numbers

递归解法,注意传递参数的方式。 class Solution { public int sumRootToLeaf(TreeNode root) { int res = 0; res = sum(root,res); return res; } public int sum(TreeNode root,int sum){ ...

xiaobailaji的博客 127

(Java)leetcode-1022 Sum of Root To Leaf Binary Numbers(从根到叶的二进制数之和)

求二叉树中,根至叶子的二进制数之和

不思量 255

LeetCode 1022. Sum of Root To Leaf Binary Numbers

【代码】LeetCode 1022. Sum of Root To Leaf Binary Numbers。

weixin_42383726的博客 172
上一篇: Leetcode 988. Smallest String Starting From Leaf (二叉树遍历好题)
下一篇: Leetcode 1457. Pseudo-Palindromic Paths in a Binary Tree (二叉树遍历题)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值