当前位置:网站首页>从根到叶的二进制数之和
从根到叶的二进制数之和
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),递归使用的栈空间。
边栏推荐
- 使用redis c库,异步内存泄露的问题
- URDF_ Xcaro
- Notes are a little fun
- Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
- Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
- Plato farm brings a new experience to community users through the LAAS protocol elephant swap
- Plato farm is expected to further expand its ecosystem through elephant swap
- Process analysis of object creation
- [Yugong series] July 2022 go teaching course 018 switch of branch structure
- Programming implementation of eight queens
猜你喜欢

11. Zuul routing gateway

Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment

Chapter 5 decision tree and random forest practice

Maximum subarray cumulative sum less than or equal to K

小于等于K的最大子数组累加和

Golang sends email to the mail Library

Subject 3: Jinan Zhangqiu line 6

3381. 手机键盘(清华大学考研机试题)

JVM原理简介

Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
随机推荐
基于风能转换系统的非线性优化跟踪控制(Matlab代码实现)
leetcode:433. 最小基因变化
分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
2022年危险化学品经营单位主要负责人复训题库及答案
Threads and processes
Towhee weekly model
Golang发送邮件库email
Nonlinear optimal tracking control based on wind energy conversion system (realized by matlab code)
A. Parkway Walk
三种常见的移动底盘运动学模型分析
Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
11. Zuul routing gateway
物联网智能家居项目---智能卧室
An online duplicate of a hidden bug
C how to set different text watermarks for each page of word
「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣
Golang sends email to the mail Library
Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)
第五届强网杯全国网络安全挑战赛 题目复现(有题目附件,详解)
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?