Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Sum of binary numbers from Root to Leaf by leetcode

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces leetcode how to solve the problem of the sum of binary numbers from root to leaf, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

The topic gives a binary tree on which the value of each node 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 the binary number 01101, which is 13. For each leaf on the tree, we have to find out the number represented by the path from the root to the leaf. Returns the sum of these numbers using 10 ^ 9 + 7 as a module. Example:! [] (https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/05/sum-of-root-to-leaf-binary-numbers.png) input: [1Jing 0Jing 1JEI 0JEI] output: 22 explanation: (100) + (101l) + (11010) + (1111) = 4 + 5 + 6 + 7 = 22 hint: the number of nodes in the tree is between 1 and 1000. Node.val is 0 or 1. Source: LeetCode link: https://leetcode-cn.com/problems/sum-of-root-to-leaf-binary-numbers copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right * / class Solution {public int sumRootToLeaf (TreeNode root) {return sumNode (root, 0);} public int sumNode (TreeNode node, int sum) {if (node = = null) {return 0;} sum = 2 * sum + node.val; if (node.left = = null & & node.right = = null) {return sum } return sumNode (node.left, sum) + sumNode (node.right, sum);}} Thank you for reading this article carefully. I hope the article "leetcode how to solve the problem of the sum of binary numbers from root to leaf" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report