题目

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. 恕我直言,你可能连 GitHub 搜索都不会用 - 如何精准搜索的神仙技巧

    大家好,我是你们的 前端章鱼猫,一个不喜欢喵.又不喜欢吃鱼的超级猫 ~ 今天给大家带来的是 在 GitHub 上如何精准搜索的神仙技巧. [前端GitHub:https://github.com/bi ...

  2. mysql全备、增量备份脚本

     1.mysql全量备份及定时删除备份文件脚本 #!/bin/bash v_user="root" v_password="mysql" backup_date ...

  3. 大数据组件Kerberos安全访问关键代码

    版本信息 <version.hbase>2.1.0-cdh6.2.1</version.hbase> <version.hadoop>3.0.0-cdh6.2.1& ...

  4. Flink问题1

    flink问题1 报错: More buffers requested available than totally available 查看源码: /** * This method makes s ...

  5. 持久层之 MyBatis: 第二篇 :动态SQL And多表查询

    MyBatis入门到精通 完整CRUD UserDaoImpl 编写UserDao对应的UserDaoMapper.xml 添加UserDao的测试用例 编写UserDao的测试用例 解决数据库字段名 ...

  6. canvas可视化效果之内阴影效果

    canvas可视化效果之内阴影效果 楔子 在之前的一个轨道交通可视化项目中,运用到了很多绘制技巧. 可以参考 之前的一篇文章 <利用canvas阴影功能与双线技巧绘制轨道交通大屏项目效果> ...

  7. Multipass使用教程

    一.Multipass介绍 Multipass是一种简单的虚拟机工具.它不仅使启用虚拟机变得快速简易,还使管理那些虚拟机变得异常简单,因此可以立即开始针对云.边缘.物联网或任何一种类型的技术进行开发. ...

  8. Ubuntu Server 16.04.1 LTS 64位 搭建LNMP环境

    安装配置 Nginx 为了确保获得最新的 Nginx,先使用sudo apt-get update命令更新源列表.安装 Nginx,输入命令:sudo apt-get install nginx. 启 ...

  9. 使用代码管理工具(git)管理代码的常用指令合集

    create a new repository on the command line echo "# test" >> README.md git init git ...

  10. Redis集群搭建采坑总结

    背景 先澄清一下,整个过程问题都不是我解决的,我在里面就是起了个打酱油的角色.因为实际上我负责这个项目,整个过程也比较清楚.之前也跟具体负责的同事说过,等过段时间带他做做项目复盘.结果一直忙,之前做的 ...