leecode 1022. Sum of Root To Leaf Binary Numbers

2024-05-07 11:32

本文主要是介绍leecode 1022. Sum of Root To Leaf Binary Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目: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 starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

Return the sum of these numbers.
Example 1:

avatar
Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22

Note:

  1. The number of nodes in the tree is between 1 and 1000.
  2. node.val is 0 or 1.
  3. The answer will not exceed 2^31 - 1.

解题思路:
1)首先记录根节点编码,例如最左点的根节点编码为:100;
2)将编码的二进制转化为10进制;
3)左右值累加,汇总到根节点。

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public://将编码的二进制转化为10进制;int binInt(string str){   int sum = 0 , binI = 1 ;int lenS = str.length() , c_i ;for (c_i = lenS - 1 ; c_i > -1 ; c_i --){ if (str[c_i] == '1')sum += binI ;binI <<= 1;sum %= 1000000007 ;binI %= 1000000007 ;     }return sum ;}int binNum(TreeNode* root , string str){if (root == NULL)return 0 ;str += to_string(root->val) ;//获取根节点的编码,将编码转化为10进制;if (root->left == NULL && root->right == NULL)return binInt(str) ;else return (binNum(root->left , str) + binNum(root->right , str)) % 1000000007 ;}int sumRootToLeaf(TreeNode* root) {string retI = "" ;if (root == NULL)return 0 ;else if (root->left == NULL && root->right == NULL)return root->val ;else return binNum(root , retI) % 1000000007;}
};

这篇关于leecode 1022. Sum of Root To Leaf Binary Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967224

相关文章

Go语言中的go.mod与go.sum

问题1:什么是go.mod以及它是用来解决什么问题的? go mod 是 Go 语言引入的包管理工具,用于解决 Go 语言项目在依赖管理方面的问题。 传统上,若不使用go mod,则需要要通过GOPATH来管理依赖,而这种方式存在一些问题: 如1. 包管理依赖不明确 2. 依赖库的版本管理 3. 需要手动管理同步依赖的复杂性等 而go mod可以帮助开发者在项目中明确管理依赖的版

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

[leetcode] 107. Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II 描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example

[leetcode] 102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal 描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20

[leetcode] 257. Binary Tree Paths

* Binary Tree Paths* 描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1->2->5”, “1->3”] 我的代码

力扣SQL50 每月交易 I 求和 SUM(条件表达式) DATE_FORMAT(日期,指定日期格式)

Problem: 1193. 每月交易 I 👨‍🏫 参考题解 Code select DATE_FORMAT(trans_date, '%Y-%m') AS month,country,count(*) as trans_count,count(if(state = 'approved', 1, NULL)) as approved_count,sum(amount) as

力扣SQL50 平均售价 ifnull SUM 连表查询

Problem: 1251. 平均售价 👨‍🏫 参考题解(题目数据增强,代码只能过90%的点) 🍻 AC code SELECT p.product_id, ROUND(ifnull(SUM(units * price) / SUM(units), 0),2) AS average_priceFROM prices as pLEFT JOIN unitsSold as u

Binary Tree Postorder Traversal-二叉树的后序遍历

原题: Given a binary tree, return the postorder traversal of its nodes' values. =>给定一个二叉树,给出后序遍历的所有节点值 For example: =>例如 Given binary tree {1,#,2,3}, =>给定一个二叉树{1,#,2,3} 1\2/3 return [3,2,1

Binary Tree Preorder Traversal--二叉树的先序遍历

原题: Given a binary tree, return the preorder traversal of its nodes' values. =>给出一个二叉树,返回先序遍历的所有的节点值 For example: 例如: Given binary tree {1,#,2,3}, 给出下面的二叉树 1\2/3 return [1,2,3]. 返回[1,2,3]

【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根据先序序列和中序序列恢复二叉树

题目:Construct Binary Tree from Preorder and Inorder Traversal <span style="font-size:18px;">/**LeetCode * 题意:给定一个二叉树的先序遍历和中序遍历的序列,重建二叉树* 思路:先序遍历的第一个节点是父节点,中序遍历从第一个节点到父节点的都是父节点的左子树,父节点后面的都是父节点的右子树*