Leetcode 1022. Sum of Root To Leaf Binary Numbers
dfs
class Solution:
def sumRootToLeaf(self, root: TreeNode) -> int:
stack=[(root,0)]
ans=[]
bi_str=[]
while stack:
node,level=stack.pop()
s_len=len(bi_str)
if s_len-1<level:
bi_str.append(str(node.val))
else:
bi_str[level]=str(node.val)
del bi_str[level+1:]
if (not node.left) and (not node.right):
ans.append(int(''.join(bi_str),2))
if node.right:
stack.append((node.right,level+1))
if node.left:
stack.append((node.left,level+1))
return sum(ans)
Leetcode 1022. Sum of Root To Leaf Binary Numbers的更多相关文章
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【leetcode】1022. 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 n ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...
- LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)
这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...
- [Swift]LeetCode1022. 从根到叶的二进制数之和 | 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 ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- java 多线程 day17 Exchanger
import java.util.concurrent.Exchanger;import java.util.concurrent.ExecutorService;import java.util.c ...
- idea中添加模板。
1:点击File>settings>live template 2: 在 Editor界面下,点击右上角 + 好, 如果想添加一个新类型的语言,点击templateGroup 输入组名. ...
- makefile中ifeq与ifneq dev/null和dev/zero简介 dd命令
ifeq语法是ifeq "<arg1>;" "<arg2>;" ,功能是比较参数“arg1”和“arg2”的值是否相同,相同时为1 i ...
- Web前台学习总结
前台的技术有很多种,流行的框架也是枚不胜举,在这里我们只讨论html,css,js这些基本的技术,相信大家如果掌握了这些最基本的技术,其他的技术也就会使用了. 下面是一个案例的形式来讲解上述的技术. ...
- mongo增删改查封装(C#)
Framework版本:.Net Framework 4 ConnectionUtil源码参见:https://www.cnblogs.com/threadj/p/10536273.html usin ...
- 20145311 《Java程序设计》第2周学习总结
20145311 <Java程序设计>第2周学习总结 教材学习内容总结 3.1Java的类型分为基本类型(Primitive type)和类类型(Class type)基本类型: *整数: ...
- Spring Data JPA 关系映射(一对一,一对多,多对多 )
CascadeType.REMOVE 级联删除操作,删除当前实体时,与它有映射关系的实体也会跟着被删除.CascadeType.MERGE 级联更新(合并)操作,当Student中的数据改变,会相应地 ...
- HDU - 3068 最长回文(马拉车Manacher)题解
思路:马拉车裸题,我们用一个p[i]数组代表以i为中心的最大回文半径.这里用了一个小技巧,如果一个串是aaaa这样的,那我们插入不相干的字符使它成为#a#a#a#a#,这样无论这个串是奇数还是偶数都会 ...
- DataSet 和 DataTable 以及 DataRow
向DataSet中添加DataTable 会提示datatable已属于另一个dataset 本来的想法是每次都new一个DataTable,但是还是会报错 百度了一下,发现可以调用DataTable ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...