当前位置:网站首页>从根到叶的二进制数之和
从根到叶的二进制数之和
2022-07-27 02:50:00 【利刃Cc】
1022. 从根到叶的二进制数之和
难度简单212
给出一棵二叉树,其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。
- 例如,如果路径为
0 -> 1 -> 1 -> 0 -> 1,那么它表示二进制数01101,也就是13。
对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。
返回这些数字之和。题目数据保证答案是一个 32 位 整数。
示例 1:

输入:root = [1,0,1,0,1,0,1]
输出:22
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
示例 2:
输入:root = [0]
输出:0
提示:
- 树中的节点数在
[1, 1000]范围内 Node.val仅为0或1
思路:递归后序遍历
看到二叉树需要遍历,最开始要想到的就是递归,将大问题拆分为小问题,让左右子树分别去完成任务,依次递归下去,直到遇到nullptr。
- 因为需要统计总和,所以定义了一个全局变量 sum ,以及考虑到递归到左右子树也需要将目前路径的值的和传过去,所以新建一个子函数负责完成递归,设置参数为 root 和 val,val 表示在遇到当前节点前的所有路径之和。
- 然后继续后序遍历:
- 若当前节点为叶子节点,则将 val 的值赋给 sum, 并返回。
- 若当前节点为非叶子节点,则继续往左右子树递归。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int sum = 0;
int _sumRootToLeaf(TreeNode* root, int val)
{
if(root == nullptr)
return 0;
val = root->val + (val << 1);
_sumRootToLeaf(root->left, val);
_sumRootToLeaf(root->right, val);
//若为叶子节点,则让sum加上这个路径的总和
if(root->left == nullptr && root->right == nullptr)
sum += val;
return sum;
}
int sumRootToLeaf(TreeNode* root) {
return _sumRootToLeaf(root, 0);
}
};
复杂度
- 时间复杂度:O(N),N是二叉树节点个数。
- 空间复杂度:O(N),递归使用的栈空间。
边栏推荐
- Subject 3: Jinan Zhangqiu line 2
- DataX无法连接对应的数据库(windows下可以,linux下失败)
- NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品
- Article main content extraction software [based on NLP technology]
- 分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
- Startup process and rescue mode
- 288 page 180000 word intelligent campus overall design directory
- The problem that prevented the installation of umi4 for one day has been solved
- NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
- VR全景制作在家装行业是谈单利器?这是为什么呢?
猜你喜欢

Skywalking distributed system application performance monitoring tool - medium

Restful fast request 2022.2.2 release, supporting batch export of documents

Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)

VR全景制作在家装行业是谈单利器?这是为什么呢?

SkyWalking分布式系统应用程序性能监控工具-中

Nonlinear optimal tracking control based on wind energy conversion system (realized by matlab code)

Startup process and rescue mode

Function pointer and callback function

电商系统结合商品秒杀活动,VR全景不断带来收益

第五届强网杯全国网络安全挑战赛 题目复现(有题目附件,详解)
随机推荐
C语言学习笔记 —— 内存管理
The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
Abstract intelligent extraction [based on Bert technology]
Subject 3: Jinan Zhangqiu line 6
Bean Validation原理篇--07
LeetCode 第二十七天
Use tag tag in golang structure
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
A. YES or YES?
Threads and processes
Is it safe for tongdaxin to open an account
这个FlinkCDC会监控数据库中所有的表?还是指定的表呢?我看后台日志,他是监控了所有表,如果监控
356页14万字高端商业办公综合楼弱电智能化系统2022版
mysql中case when返回多个字段处理方案
Redis(九) - Redis之分布式锁
Skywalking distributed system application performance monitoring tool - medium
酷雷曼VR全景为你铺设创业之路
H.265网页播放器EasyPlayer对外开放录像的方法
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?
一维数组的应用