1022. Sum of Root To Leaf Binary Numbers

2023-12-21 16:38
文章标签 sum numbers binary 1022 leaf

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

1022. 从根到叶的二进制数之和

给出一棵二叉树,其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。例如,如果路径为 0 -> 1 -> 1 -> 0 -> 1,那么它表示二进制数 01101,也就是 13 。

对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。

 10^9 + 7 为,返回这些数字之和。

 

示例:

输入:[1,0,1,0,1,0,1]
输出:22
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22

 

提示:

  1. 树中的结点数介于 11000 之间。
  2. node.val 为 0 或 1 。

解法一

//时间复杂度O(n), 空间复杂度O(logn)
/*** 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:void sumRootToLeaf(TreeNode* root, int cur, int& sum) {if(!root) return;if(!root->left && !root->right) sum += (cur %= 1000000007) * 2 + root->val;sumRootToLeaf(root->left, (cur %= 1000000007) * 2 + root->val, sum);sumRootToLeaf(root->right, (cur %= 1000000007) * 2 + root->val, sum);}int sumRootToLeaf(TreeNode* root) {int sum = 0;sumRootToLeaf(root, 0, sum);return sum;}
};

思路:

DFS遍历。当前二进制前缀构成的数字对1000000007取余记为cur,另一变量sum记录所求的和。

对于递归遍历的每一层,都先令cur %= 1000000007,再乘以2(左移1位),加上root->val(低位),所得的结果传入下一层。这一步骤与1018类似。

2019/09/05 22:51

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



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

相关文章

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 * 题意:给定一个二叉树的先序遍历和中序遍历的序列,重建二叉树* 思路:先序遍历的第一个节点是父节点,中序遍历从第一个节点到父节点的都是父节点的左子树,父节点后面的都是父节点的右子树*