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 ...
随机推荐
- PAT 1138 Postorder Traversal [比较]
1138 Postorder Traversal (25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- java 的==和equals的区别(二)
java 的==和equals的区别 java 的==和equals的区别 ==通常表明引用的是同一个东西(引用的地址相同),equals通常表明两个对象的内容相同(值相同) ------------ ...
- ReactNative Ios报出 'React/RCTBundleURLProvider.h' file not found错误
我在创建react-native项目时 npm了一个第三方库 结果一打开 xcode 竟然报错 React/RCTBundleURLProvider.h' file not found: 然后 我 ...
- db2,oracle,mysql ,sqlserver限制返回的行数
不同数据库限制返回的行数的关键字如下: ①db2 select * from table fetch first 10 rows only; ②oracle select * from table w ...
- deepin中crossover或playonlinux装完office后word无法输入中文的问题
原因:office安装是自带了一个微软输入法 解决:装offce时进行自定义安装,在office共享功能里,把输入法去掉. 参考: https://jingyan.baidu.com/article/ ...
- Labeled Faces in the Wild 人脸识别数据集
http://blog.csdn.net/garfielder007/article/details/51480525 New (draft) survey paper: Labeled Faces ...
- 20145106 《Java程序设计》第1周学习总结
20145106 <Java程序设计>第1周学习总结 教材学习内容总结 因为我用的是Mac,所以教材内容暂时对我的编译java没有帮助.不过还好我也在同学和自己的帮助学习下初步学会了在Ma ...
- Ubuntu下录制屏幕并转换成gif【转】
本文转载自:https://blog.csdn.net/u012964944/article/details/50464263 *录制屏幕 1)打开Ubuntu软件中心,安装RecordMyDeskt ...
- How to install tensorflow from source on ubuntu 18.04 64bit
1,install dependencies sudo apt-get install openjdk-8-jdk git python-dev python3-dev python-numpy py ...
- php 数值数组遍历
<?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); ...