LeetCode1022. 从根到叶的二进制数之和
题目
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. 从根到叶的二进制数之和的更多相关文章
- [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 ...
- 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 ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- [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 ...
- LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)
这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...
- [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 ...
- C语言递归之求根到叶节点数字之和
题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...
- 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 ...
- 树——sum-root-to-leaf-numbers(根到叶节点数字之和)
问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...
随机推荐
- xwiki升级8.8.4
安装包下载: http://download.forge.ow2.org/xwiki/xwiki-enterprise-jetty-hsqldb-8.4.4.zip 推荐使用jetty包,方便快捷,不 ...
- 使用脚手架搭建vue项目
全局安装环境 安装webpack npm install webpack -g 安装vue脚手架 npm install -g @vue/cli-init 初始化vue项目 vue init webp ...
- springmvc中ModelAttribute注解应用在参数中
可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...
- Flutter AS设备连接显示loading解决方案
看了网上很多解决方案,基本都是要杀dart进程后,删除lockfile 文件,然后运行检查命令flutter doctor. 这个方式有一定的意义,但是确实不一定解决这个问题. 今天就遇到了这样的问题 ...
- [OI笔记]后缀自动机
本来没打算写的,不过想想看后缀自动机的理论看了两三天了才有点懂(我太傻了)-下周期末考的话大概要去复习一下文化课感觉回来又要忘得差不多,还是开篇blog记一下好了. 相关的资料: cls当年的课件:2 ...
- Clickhouse的特点
1.为什么会有Clickhouse? 实时数据分析数据库 俄罗斯的谷歌开发的. 2.Clickhouse的优点. 真正的面向列的 DBMS ClickHouse 是一个 DBMS,而不是一个单一的数据 ...
- Python进阶——什么是上下文管理器?
在 Python 开发中,我们经常会使用到 with 语法块,例如在读写文件时,保证文件描述符的正确关闭,避免资源泄露问题. 你有没有思考过, with 背后是如何实现的?我们常常听到的上下文管理器究 ...
- 安装篇七:配置 Nginx 使其支持 PHP 应用
配置说明(NGINX-PHP) (让nginx php(中间件)之间建立关系):nginx--php建立关系---fastcgi---cgi 第一个里程: 编写nginx虚拟主机配置文件 第二个里程 ...
- Spring-Boot配置文件web性能(服务器)配置项(常用配置项为红色)
参数 介绍 server.address 服务器应绑定到的网络地址 server.compression.enabled = false 如果启用响应压缩 server.compression.exc ...
- 在wildfly 21中搭建cluster集群
目录 简介 下载软件和相关组件 配置domain 创建应用程序 部署应用程序 集群配置 总结 简介 wildfly是一个非常强大的工具,我们可以轻松的使用wildfly部署应用程序,更为强大的是,wi ...