当前位置:网站首页>Force deduction solution summary 1022- sum of binary numbers from root to leaf
Force deduction solution summary 1022- sum of binary numbers from root to leaf
2022-06-12 02:09:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link :
Power button
describe :
Give me a binary tree , The value of each node on it is 0 or 1 . Each path from root to leaf represents a binary number starting from the most significant bit .
for example , If the path is 0 -> 1 -> 1 -> 0 -> 1, Then it represents a binary number 01101, That is to say 13 .
For every leaf on the tree , We all have to find the number represented by the path from the root to the leaf .
Returns the sum of these numbers . The question data guarantees that the answer is 32 position Integers .
Example 1:
Input :root = [1,0,1,0,1,0,1]
Output :22
explain :(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Example 2:
Input :root = [0]
Output :0
Tips :
The number of nodes in the tree is [1, 1000] Within the scope of
Node.val Only for 0 or 1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Recursively traverse all nodes . Every time you traverse a node , Accept the accumulated value passed from the root node , Then add the current value to find the value of the current node . * If the node is a leaf node , The calculation SUM value . * If not , Pass the value of the current node and recurse .
Code :
public class Solution1022 {
int sum = 0;
public int sumRootToLeaf(TreeNode root) {
ergodic(root, 0);
return sum;
}
private void ergodic(TreeNode root, int i) {
int current = (i << 1) + root.val;
if (root.right == null && root.left == null) {
sum += current;
return;
}
if (root.left != null) {
ergodic(root.left, current);
}
if (root.right != null) {
ergodic(root.right, current);
}
}
}边栏推荐
- Google Ads 竞价的运作机制
- How to stop anti-virus software from blocking a web page? Take gdata as an example
- Swiftyjson parsing local JSON files
- 消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
- 商城开发知识点
- 力扣解法汇总1022-从根到叶的二进制数之和
- How can low code platforms improve cost effectiveness?
- ACL 2022 | 预训练语言模型和图文模型的强强联合
- 力扣解法汇总417-太平洋大西洋水流问题
- 力扣解法汇总668-乘法表中第k小的数
猜你喜欢

leetcodeSQL:612. Nearest distance on plane

Why do we use Google search ads?

ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法

The establishment and introduction of the announcement module of PHP development blog system

Does the virtual host have independent IP

MySQL table common operation mind map

How to maximize the use of various matching methods—— Google SEM

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
![[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology](/img/16/2f9a235995cdd54ac9b85a68a7afcb.jpg)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology

消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
随机推荐
Does the virtual host have independent IP
A mystery of the end of vagrant up
力扣解法汇总875-爱吃香蕉的珂珂
Design principle [Demeter's Law]
SQL calculates KS, AUC, IV, psi and other risk control model indicators
The establishment and introduction of the announcement module of PHP development blog system
MySQL高级部分知识点
打包一个包含手表端应用的手机端APK应用—Ticwear
xcall 集群脚本(查看jps命令)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
MySQL表常用操作思维导图
virsh创建/关闭/停止虚拟机常用的几条指令
Leetcode 45 jump game II
MySQL table common operation mind map
Force deduction solution summary 965- single valued binary tree
Is the bidding price fixed for each click?
C asynchronous programming from simple to deep (III) details awaiter
力扣解法汇总462-最少移动次数使数组元素相等 II
Swiftyjson analyse les fichiers json locaux
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)