leetcode:Sum Root to Leaf Numbers

36、T5L 迪文屏C51开发之绘制动态曲线 T5L 迪文屏C51开发之绘制动态曲线1、介绍2、例程功能简介3、GUI界面设计3、C51程序设计 1、介绍   本文将利用DGUS Tool软件中的【动态曲线控件】来绘制曲线效果,在T5L迪文屏中总共支持8个通道的曲线,而曲线数据是需要缓冲区的,如果 8 通道曲线同时使用,0x1000-0x4FFF 将作为曲线缓冲区地址,此时该部分变量地址不能被其他控键使用,其他控键地址使用范围为0x5000-0xFFFF,当某曲线通道不被使用时,那么其所对应的缓冲区地址可以做其他用途,曲线通道具体的地址分布如下图所示: 阅读详情

Problem:

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   / \
  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

Solution:

看到这个的时候就顺便先复习了一下二叉树的前序遍历、中序遍历、后序遍历的递归实现以及非递归实现,复习了之后,就很自然的用后序遍历解决了这道题。思路就是从树的底层开始向上,依次的计算当前节点的权重,就是最后加和中的权重。这个我修改了val的值为以当前节点为根的树的sumNumber值,这样的好处是不用再增加其他的变量来存储了。就这么简单,写完调试好后一次性通过。代码如下

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (root==NULL) return 0;
        stack<pair<TreeNode *,bool>* > s;
        TreeNode *p=root;
        pair<TreeNode *,bool> *sp;
        map<TreeNode *,int> weight;
        while(p!=NULL || !s.empty())
        {
            while(p!=NULL)
            {
                sp=new pair<TreeNode *,bool>(p,true);
                s.push(sp);
                p=p->left;
            }
            if (!s.empty())
            {
                sp=s.top();
                s.pop();
                if (sp->second)
                {
                    sp->second=false;
                    s.push(sp);
                    p=sp->first->right;
                }
                else
                {
                    if (sp->first->left ==NULL && sp->first->right==NULL)
                    {
                        weight[sp->first]=1;
                        sp->first->val=sp->first->val*weight[sp->first];
                    }
                    else if (sp->first->left!=NULL && sp->first->right==NULL)
                    {
                        weight[sp->first]=weight[sp->first->left]*10;
                        sp->first->val=sp->first->val*weight[sp->first]+sp->first->left->val;
                    }
                    else if (sp->first->left==NULL && sp->first->right!=NULL)
                    {
                        weight[sp->first]=weight[sp->first->right]*10;
                        sp->first->val=sp->first->val*weight[sp->first]+sp->first->right->val;
                    }
                    else
                    {
                        weight[sp->first]=weight[sp->first->right]*10+weight[sp->first->left]*10;
                        sp->first->val=sp->first->val*weight[sp->first]+sp->first->left->val+sp->first->right->val;
                    }

                    p=NULL;
                    delete sp;
                }
            }
        }
        return root->val;
    }
};


自私兽群优化器(SHO)详解:从生物本能到智能优化 自私兽群优化器(SHO)是一种受动物群体自私行为启发的智能优化算法。该算法模拟动物在捕食威胁下的两种行为:聚集行为(全局搜索)和自私行为(局部搜索),将优化问题的解空间映射为生存环境,候选解视为猎物个体。SHO通过安全性评估将猎物分为安全、危险和高危三类,分别采用不同的位置更新策略,并引入捕食者角色增加选择压力。相比传统优化算法,SHO具有无需梯度信息、自适应平衡探索与开发、参数简单等优势,特别适合高维复杂和多模态优化问题。算法通过马尔可夫链理论保证收敛性,已在工程设计和数据挖掘等领域展现出良好性能。 阅读详情

相关推荐

STM32F103C8T6的PC13-15引脚配置避坑指南:为什么你的IO口不工作?

本文详细解析了STM32F103C8T6的PC13-15引脚作为普通IO口不工作的原因及解决方案。这三个引脚默认关联侵入检测和低速外部时钟等特殊功能,需先禁用TAMPER和LSE,并开启AFIO时钟,才能正确配置为普通GPIO。文章提供了标准外设库和HAL库的配置步骤、常见问题排查方法及硬件设计注意事项。

weixin_29169899的博客 522

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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the

给永远比拿愉快 1631

西门子V90 PN伺服控制核心:FB284功能块详解与实战

在工业自动化中,伺服定位控制是运动控制的基础环节,其本质是PLC通过PROFINET协议与伺服驱动器实现高速、确定性数据交互。FB284作为西门子为V90 PN定制的标准化功能块,封装了GSD映射、状态机管理与周期性PDO同步机制,将底层寄存器操作抽象为高可靠调用接口。相比MOVE指令或MC_MoveAbsolute等通用方案,FB284显著降低时序错乱风险,提升多轴同步精度与故障响应能力。它广泛应用于包装、灌装、装配等中端产线,尤其适配S7-1200/1500平台下的快速部署与现场维护。本文聚焦V90 P

weixin_42696271的博客 299

[LeetCode] 113: Sum Root to Leaf Numbers

[Problem]Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the to

1.02^365的成长裂变 3640

Stanford CS230深度学习(一)

斯坦福CS230可以作为深度学习的入门课,最近我也在跟着看视频、完成编程作业。首先列出使用的资源链接,然后给出第一课的理解和编程作业的代码。 所有资料如下: 一、课程连接: b站课堂讲授版:Stanford CS230(吴恩达 深度学习 Deep Learning | Autumn 2018)(中英双字幕) (一共10个视频,每周一个,但是每个视频对应下面的配套视频较多) Coursera配套视...

weixin_44750583的博客 2381

电压抬升电路设计

一、实验目的 掌握集成运放电压放大电路设计基本方法。 掌握基本仪器使用方法(电源、信号发生器、示波器)。 二、实验内容及结果 实验内容 基于集成运放设计一传感器信号采集电路。传感器输出信号在±50mV,频率为100Hz以内,模数转换器允许输入电压在0~5V,要求设计传感器与模数转换器之间的信号采集电路,实现传感器信号放大10倍,并适合模数转换器的输入。模数转换器的输入电阻为1KHz,设计电路时,可使用1KHz电阻作为信号采集电路负载,代替模数转换器。 实验具体要求如下: (1)确定电路形式,说明电压转

Safaearth的博客 4万+

leetcode笔记:Sum Root to Leaf Numbers

这道题只是一道二叉树的深度优先搜索的题目,在叶结点时将从根到叶结点的路径上的结点的值组成一个十进制的数,本质上还是一道深度优先搜索的题。

liyuefeilong的专栏 1788

WebRTC之beamforming算法

CSND已永久停更,最新版唯一来源点击下面链接跳转: 语音增强和语音识别网页书 坐标问题 这里以笔记本为例进行说明,当笔记本按如下方式打开: 首先是键盘和液晶屏呈90度,然后鼠标上方,也就是液晶屏上方正中间是camera,把平行于键盘的面看成是xy平面,把液晶屏所在的面看成是xz平面,所有平面的原点都在Camera的位置。 方位角:从原点开始,Camera的右侧是0度,逆时针方...

shichaog的专栏 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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

363

YOLOv13改进策略【特征提取层】| 引入注意力卷积模块RFAConv,关注感受野空间特征 助力yolov13精度提升

RFAConv:创新空间注意力和标准卷积运算感受野空间特征是针对卷积核设计的,根据卷积核大小动态生成。以3×3卷积核为例,它由非重叠滑动窗口组成,每个窗口代表一个感受野滑块。

Limiiiing的博客 360

倍福EL5101模块连接编码器

1、 EL5101模块简介:EL5101为单通道增量型编码器计数模块,一共有EL5101-0000、EL5101-0010、EL5101-0011、EL5101-0090(尚未供货)四种型号,其中EL5101-0000比较常用,本例使用EL5101-0000连接增量型编码器 2、 硬件外观和接线 在这里插入图片描述 3、 指示灯说明 4、和编码器连接的案例 ......

weixin_41883890的博客 6501

Leetcode:Sum Root to Leaf Numbers

URLhttps://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 represent a number.An example is the root-to-le

zhumingyuan111的博客 231

C语言设计模式全面解析:从理论到28个实战示例

本文系统介绍了23种经典设计模式在C语言中的实现方法。通过结构体、函数指针等技巧,C语言可以优雅地模拟面向对象特性,实现代码复用与解耦。文章分类详解了创建型、结构型、行为型三大类模式,并提供了28个完整代码示例,包括单例、策略、观察者等核心模式的精简实现。这些模式在嵌入式系统、操作系统内核等场景中广泛应用,如状态机、事件驱动等。学习C语言设计模式的关键在于理解模式意图,掌握函数指针和结构体组合等技巧,并在实际项目中灵活运用。本文为C语言开发者提供了全面的设计模式参考指南。

数字人生 363

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. An example is the root-to-leaf path 1->2->3 which represents the number 123.

SunliyMonkey 1172

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode: Sum Root to Leaf Numbers 解题报告

Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1-&gt;2-&gt;3 which represents the n...

weixin_33933118的博客 151

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode129:Sum Root to Leaf Numbers

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Fin...

diehuojiang5959的博客 152

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

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

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.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of al

EmilSinclair4391的博客 454

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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total ...

qq_34314736的博客 114

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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

博客已退休 369

《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.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of

wojiushimogui的博客 546

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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

le119126的专栏 450

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. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

henryzhihua 2105

Leetcode: Sum Root to Leaf Numbers

QuestionGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total s

ayst123 的专栏 338

LeetCode 129: Sum Root to Leaf Numbers

Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.

一只码畜 325
上一篇: leetcode:Surrounded Regions
下一篇: leetcode:Longest Consecutive Sequence
binary_search
博客等级 码龄14年 6粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值