边工作边刷题:70天一遍leetcode: day 76
Count Univalue Subtrees
要点:检测条件比较有意思:因为可能的情况比较多,只要违反了任意一条就return False,所以可以只考虑False的情况,最后return True。
https://repl.it/CoqQ
- 错误点:这题类似Largest BST Subtree(with all descendants,bottom up方法,or post order),左子树不符合只能invalidate root,但仍然要recurse到右子树,所以不能提前返回,而是要在右子树访问完以后返回。
post-order iteration: 注意必须要用map记录,因为post-order没法从栈中获得当前结点左/右子树的情况,所以只能用map记录
https://repl.it/CopV
# 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 countUnivalSubtrees(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def helper(root):
isUnival = True
if root.left and (not helper(root.left) or root.val!=root.left.val):
isUnival = False
if root.right and (not helper(root.right) or root.val!=root.right.val):
isUnival = False
if not isUnival:
return False
#print root.val
self.count+=1
return True
self.count=0
if not root: return 0
helper(root)
return self.count
# Given a binary tree, count the number of uni-value subtrees.
# A Uni-value subtree means all nodes of the subtree have the same value.
# For example:
# Given binary tree,
# 5
# / \
# 1 5
# / \ \
# 5 5 5
# return 4.
# Hide Tags Tree
# 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 countUnivalSubtrees(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
stk = [root]
umap = {}
pre = None
count = 0
while stk:
cur = stk[-1]
if pre==None or pre.left==cur or pre.right==cur:
if cur.left:
stk.append(cur.left)
elif cur.right:
stk.append(cur.right)
elif cur.left==pre:
if cur.right:
stk.append(cur.right)
else:
stk.pop()
if cur.left and (not umap[cur.left] or cur.left.val!=cur.val):
pre = cur
umap[cur]=False
continue
if cur.right and (not umap[cur.right] or cur.right.val!=cur.val):
pre = cur
umap[cur]=False
continue
umap[cur]=True
count+=1
pre = cur
return count
边工作边刷题:70天一遍leetcode: day 76的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- 开源项目Foq简介
Foq是一个轻量级-线程安全的mocking类库.使用它来mock抽象类与接口这是我们通常的做法.Foq的名字来自Moq,如果你使用过Moq的话,自然后联想到它能做什么.Foq主要是为了F#的 ...
- dbcp 1.4 底层连接断开时内存泄露bug
在dbcp 1.4中,如果底层的连接已经与数据库断开了,此时dbcp 1.4的实现并不释放内部连接,虽然早已提供了removeAbandoned和removeAbandonedTimeout参数,但是 ...
- 六个创建模式之单例模式(Singleton Pattern)
定义: 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.三个特点:一个类只有一个实例:必需自己创建这个实例:必需自行向整个系统提供这个实例. 结构图: Singleton:单例类,提 ...
- TSQL生成Combguid
Nhibernate实现combguid /// <summary> /// Generate a new <see cref="Guid"/> using ...
- swift学习笔记之-继承
//继承 import UIKit /* 继承(Inheritance): 1.一个类可以继承(inherit)另一个类的方法(methods).属性(properties)和其它特性.当一个类继承其 ...
- 通用javascript方法
//将序列化成json格式后日期(毫秒数)转成日期格式 YYYY-MM-DD HH:MI:SS function ChangeDateFormat(cellval, type) { var date ...
- CAS实现单点登入(sso)经典教程
本教程我已按照步骤实现,不过要深入了解单点登入还需要进一步的学习,掌握其中的精髓. 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomcat7.2 JDK6 CAS Se ...
- android:descendantFocusability=”blocksDescendants”的用法
android:descendantFocusability用法简析 开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承B ...
- 最简MacOs10.8安装
虚拟机中安装Mac Os X的方法网上很多很多,但是对刚接触的朋友来讲肯定不是一件容易的事,这个自己深有体会,包括去年已经装好过,今年再找教程安装都装不起来,期间还出现了各种问题,幸好去年装好之后备份 ...
- iOS-RegexKitLite导入错误
RegexKitLite是什么? RegexKitLite是一个非常方便的处理正则表达式的第三方类库. 本身只有一个RegexKitLite.h和RegexKitLite.m 导入RegexKitLi ...