题目

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. C# 学习第七天

    P96 面向对象的概念 ①先有的 面向过程 --------> 然后才衍生出面向对象的思想 ②面向过程:面向的是完成这件事儿的过程,强调的是完成这件事儿的动作 比如说 把大象塞进冰箱去 ③面向过 ...

  2. Matplotlib处理csv文件

    csv模块包含在python标准库中,可用于分析CSV文件中的数据行,让我们能够快速提取感兴趣的值.首先绘制一个简单的折线图: 1 #!usr/bin/env python 2 #*-*Coding= ...

  3. C#中RDLC合并两个列的值

    使用 & 符号连接 =Fields!ID.Value & Fields!Name.Value

  4. sql去除重复的几种方法

    所以用这样一句SQL就可以去掉重复项了: select * from msg group by terminal_id; SQL中distinct的用法(四种示例分析) 示例1 select dist ...

  5. sklearn中的SGDClassifier

    常用于大规模稀疏机器学习问题上 1.优点: 高效 简单 2.可以选择损失函数 loss="hinge": (soft-margin)线性SVM. loss="modifi ...

  6. 求你别再用swagger了,给你推荐几个在线文档生成神器

    前言 最近公司打算做一个openapi开放平台,让我找一款好用的在线文档生成工具,具体要求如下: 必须是开源的 能够实时生成在线文档 支持全文搜索 支持在线调试功能 界面优美 说实话,这个需求看起来简 ...

  7. Kafka 消费组消费者分配策略

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  8. tabControl组件的吸顶效果

    最开始,还没有使用better-scroll插件的时候,直接在class中设定了一定的position为sticky,设置一定的top达成了效果.但是,使用better-scroll组件后,这些属性就 ...

  9. WebSocket协议 与 IO多路复用

    最近在把 Facebook Message 接入客服系统,由于与 Facebook Message 对接的收发消息都是通过调用 http 接口来实现的,如果想实现即时通讯,还需要在中间加一个 WebS ...

  10. 循序渐进VUE+Element 前端应用开发(31)--- 系统的日志管理,包括登录日志、接口访问日志、实体变化历史日志

    在一个系统的权限管理模块中,一般都需要跟踪一些具体的日志,ABP框架的系统的日志管理,包括登录日志.接口访问日志.实体变化历史日志,本篇随笔介绍ABP框架中这些日志的管理和界面处理. 1.系统登录日志 ...