题目

class Solution {
public:
int ans = 0;
int sumRootToLeaf(TreeNode* root) {
dfs(root,0);
return ans;
}
void dfs(TreeNode*root,int cur){
if(root== NULL) return ;
if(root->left == NULL && root->right == NULL){
cur += root->val;
ans += cur;
}
dfs(root->left,(cur+root->val)*2);
dfs(root->right,(cur+root->val)*2);
} };

本题值得二三刷,开始思路是混乱的,想要保存每一条到叶子结点的路径,然后结合将二进制转换成十进制

就算后来想到用先序遍历递归来做,对于递归中保存值处理的不好。

若递归中需要记录之前值,可以通过将该值的信息作为参数来进行传递。

LeetCode1022. 从根到叶的二进制数之和的更多相关文章

  1. [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 ...

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

  3. 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和

    网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...

  4. [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 ...

  5. LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)

    这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...

  6. [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 ...

  7. C语言递归之求根到叶节点数字之和

    题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...

  8. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

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

  9. 树——sum-root-to-leaf-numbers(根到叶节点数字之和)

    问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...

随机推荐

  1. Nginx(四):Keepalived+Nginx 高可用集群

    Keepalived+Nginx 高可用集群 (主从模式) 集群架构图 安装keepalived [root@localhost ~]# yum install -y keepalived 查看状态 ...

  2. 【软件测试 Python自动化】全网最全大厂面试题,看完以后你就是面试官!

    前言 为了让大家更好的理解和学习投入到Python自动化来找到一份好的资料也是学习过程中,非常重要的一个点.你的检索能力越强,你就会越容易找到最合适你的资料. 有需要的小伙伴可以复制群号 313782 ...

  3. 个人微信公众号搭建Python实现 -个人公众号搭建-运行run方法的编写(14.3.3)

    @ 目录 1.主要逻辑 2.代码 关于作者 1.主要逻辑 使用的是flask服务器 就使用一个函数处理请求 第一个是验证服务器,返回微信服务器给的字符串就表示验证成功 第二是要处理微信服务器发送过来的 ...

  4. 图片放大缩小的zoom.js

    1 +function ($) { "use strict"; 2 3 /** 4 * The zoom service 5 */ 6 function ZoomService ( ...

  5. Linux下安装ffmpeg,视频格式转换

    下载ffmpeg 从ffmpeg官网:http://ffmpeg.org/download.html 下载最新的ffmpeg安装包,然后通过如下命令解压: 解压 ffmpeg-*.tar.bz2 文件 ...

  6. C#中的深度学习(二):预处理识别硬币的数据集

    在文章中,我们将对输入到机器学习模型中的数据集进行预处理. 这里我们将对一个硬币数据集进行预处理,以便以后在监督学习模型中进行训练.在机器学习中预处理数据集通常涉及以下任务: 清理数据--通过对周围数 ...

  7. matplotlib之scatter

    Matplotlib之scatter 1,使用scatter绘制散点图并设置其样式: 1 import matplotlib.pyplot as plt 2 3 '''使用scatter绘制散点图并设 ...

  8. 扫描条形码获取商品信息(iOS 开发)

    一.导入ZBarSDK及其依赖库(这不是本文侧重点)     1.下载地址 https://github.com/bmorton/ZBarSDK     2.导入头文件 #import "Z ...

  9. MVC中Bundle的使用

    BundleConfig配置 (1)StyleBundle中的参数,即为cshtml中需要调用的虚拟路径名称. (2)Include包含路径,可以包含一个或多个css或js文件.即包含一组文件. pu ...

  10. 【代码周边】-GitHub笔记

    ------------恢复内容开始------------ 程序员的宝库github是个好东西,其中开源的项目足够我们的使用,但是如何去精准的获取我们的项目是很多初学者的问题.特别是英语不好的我,一 ...