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 ...
随机推荐
- postgresql主从复制
本文章以rpm包方式安装,版本为9.3.4 一.postgresql安装 postgresql93-9.3.4-1PGDG.rhel6.x86_64.rpm postgresql93-libs-9.3 ...
- Hadoop mapreduce自定义分组RawComparator
本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...
- uva11090 Bellman-Ford 运用
给定一一个n个点m条边的加权有向图, 平均值最小的回路. 二分答案,对于每个二分的mid 做一次Bellman-Fprd , 假设有k条边组成的回路. 回路上各条边的权值为 w1 , w2 ..wk ...
- C++之路
我学习C/C++也有两年了.开始是偏爱C语言和C++的语法特性强大,想用来做游戏开发.在深入学习的同时,逐渐了解到C++可以做很多事.大型项目需要用到运行效率高的C++,虽然运行效率越高,开发效率就要 ...
- C#反射——简单反射操作类的封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Re ...
- 如何写一个自己的渣渣PHP框架
原文链接:https://www.opqnext.com/2017/01/18/%E5%A6%82%E4%BD%95%E5%86%99%E4%B8%80%E4%B8%AA%E8%87%AA%E5%B7 ...
- 20145118 《Java程序设计》 第3周学习总结
20145118 <Java程序设计> 第3周学习总结 教材学习内容总结 第四章开始接触到了Java的核心内容---对象这个概念,在这里为避免混淆,列举面向过程和面向对象的区别: 面向对象 ...
- linux内核分析第三周-跟踪分析Linux内核的启动过程
一.实验流程 1.打开环境 执行命令:cd LinuxKernel/ 执行命令:qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd root ...
- SpringMVC小demo解析
第一次实际接触SpringMVC,之前在教程网站上看得是概念性的. SpringMVC是属于Java框架SSM中的一环 在做了一个小demo后发现原来编程如此简单. 首先建立动态网页项目(Dynami ...
- [Pytorch]Pytorch 细节记录(转)
文章来源 https://www.cnblogs.com/king-lps/p/8570021.html 1. PyTorch进行训练和测试时指定实例化的model模式为:train/eval eg: ...