Python不熟悉

不同的做法

404. Sum of Left Leaves

这是我的做法,AC。

class Solution(object):
res = 0
def recursive(self, root):
if root == None:
return
if root.left != None and root.left.left == None and root.left.right == None:
self.res += root.left.val
self.recursive(root.left)
self.recursive(root.right)
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.recursive(root)
return self.res

也可以进行迭代:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sumOfLeftLeaves(self, root):
"""
:type root: TreeNode
:rtype: int
"""
## 3rd try: iterative bfs
if not root:
return 0
self.queue = [root]
self.ans = 0
while self.queue:
n = self.queue.pop(0)
if n.left:
if n.left.left == None and n.left.right == None:
self.ans += n.left.val
else:
self.queue.append(n.left)
if n.right:
self.queue.append(n.right)
return self.ans
## 3.2: iterative dfs
'''
if not root:
return 0 self.res = 0
self.stack = [root]
while self.stack:
node = self.stack.pop() if node.left:
if node.left.left is None and node.left.right is None:
self.res += node.left.val
else:
self.stack.append(node.left)
if node.right:
self.stack.append(node.right) return self.res
'''
## 2nd try with help from discussion: recursive on local variable
'''
if root == None:
return 0
ans = 0
if root.left and root.left.left == None and root.left.right == None:
ans = root.left.val
return ans + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
'''
## 1st try: recursive on global variable
'''
self.ans = 0 def dfs(root, isLeft):
if root == None:
return
if not root.left and not root.right:
if isLeft:
self.ans += root.val
return
dfs(root.left, True)
dfs(root.right, False) dfs(root, False)
return self.ans
'''

没有理解题意

563. Binary Tree Tilt

AC版本:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
count = 0
def recursive(self, root):
if root == None:
return 0
left = self.recursive(root.left)
right = self.recursive(root.right)
self.count += abs(left - right)
return root.val + left + right
def findTilt(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.recursive(root)
return self.count

加速版本,利用tuple一个保存累积的tilt,一个保存当前结点的子结点的和:

class Solution(object):
def findTilt(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
(tree_tilt,s)=self.recurTilt(root)
return tree_tilt
def recurTilt(self,root):
#return tree tilt, and tree nodes sum
if not root:
return 0,0
(ltree_tilt,l_sum)=self.recurTilt(root.left)
(rtree_tilt,r_sum)=self.recurTilt(root.right)
node_tilt=abs(l_sum-r_sum)
return (ltree_tilt+rtree_tilt+node_tilt,l_sum+r_sum+root.val)

并不是简单的计算两个结点的值的绝对值,而是将两个结点的子结点的和求绝对值差,比如说下面这个例子:

它计算的过程应该是(5 - 0) + (4 - 0) + ((3 + 5) - (2 + 4)) ,最终的结果是11,所以像我之前没有计算当前结点的两个子结点的和都是错的。

class Solution(object):
count = 0
def recursive(self, root):
if root == None:
return 0
if root.left and root.right:
self.count += abs(root.left.val - root.right.val)
elif root.left:
self.count += root.left.val
elif root.right:
self.count += root.right.val
self.recursive(root.left)
self.recursive(root.right)
def findTilt(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
self.recursive(root)
return self.count

Python LeetCode的更多相关文章

  1. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  2. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  3. python LeetCode 两数相除

    近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...

  4. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  5. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  6. python leetcode 颠倒二进制数

    我的做法,,这个题在于必须补0 def reverseBits(n): num=32-len(bin(n)[2:]) m = bin(n)[2:][::-1] if num > 0: for i ...

  7. python(leetcode)-重复元素算法题

    leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...

  8. python(leetcode)-344反转字符串

    编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出. 不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题. 你可以 ...

  9. python(leetcode)-48旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

随机推荐

  1. Linux centOS的vm虚拟机配置详细 中文版

    这里以安装cenOS6.6 为例 如果想要需要cenos 6.6 ios文件的朋友看我的另一篇关于cenos6.6版本的下载详细 文中内容是摘抄自老男孩老师的<linux 跟老男孩学Linux运 ...

  2. webpack前端工程化构建工具的使用

    一.模块打包机 1.创建文件 在目标文件下建立一个src文件夹作为js代码区:作为例子,我创建了两个js文件,并利用commonJS规范require引入到index.js中: moduleA.js: ...

  3. watchdog(IWDG)

    1.为了避免程序忙跑跑死了没反应,加上一个看门狗watchdog实时监控着程序,一旦程序没有在规定的时间喂狗,则狗叫使得单片机复位. 2.Independent watchdog(IWDG)内部有时钟 ...

  4. 深入理解YYCache

    前言 本篇文章将带来YYCache的解读,YYCache支持内存和本地两种方式的数据存储.我们先抛出两个问题: YYCache是如何把数据写入内存之中的?又是如何实现的高效读取? YYCache采用了 ...

  5. jquery让页面滚动到底部

    function scrollToEnd(){//滚动到底部 var h = $(document).height()-$(window).height(); $(document).scrollTo ...

  6. Api接口通用安全策略及实现-OSS.Core

    这篇文章一直说写,迟迟没有动手,这两天看到一些应用接口数据被别人爬虫.短信接口被人高频率请求攻击等案列,感觉简单概述分享一下接口安全验证还是有必要的.毕竟当下基本都以客户端应用为主,如果前期疏忽,发布 ...

  7. [leetcode-599-Minimum Index Sum of Two Lists]

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  8. 我的学习之路_第二十三章_HTML

    Html : 超级文本语言 ( Hyper text Markup Language ) HTML 文件扩展名是 * .html HTML 结构都是有标签组成 通常情况下标签有开始标签和结束标签组成 ...

  9. 9.session的生命周期

    1.创建 当客户端第一次访问某个jsp或者Servlet的时候,服务器会为当前会话创建一个SessionId,每次客户端向服务端发送请求的时候,都会将此SessionId携带过去,服务端会对此Sess ...

  10. 开源 .net license tool, EasyLicense !

    介绍: 过去我常常像是否有一个帮助授权的软件,它可以非常简单的创建license,并且非常容易的验证license. 这是一个非常普通和公共的功能,但是我没有找到合适的开源软件,大部分开源软件都比较复 ...