dfs

要点是这一句:

return node.val==1 or node.left or node.right

完整代码:

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import functools
class Solution:
@functools.lru_cache()
def pruneTree(self, root: TreeNode) -> TreeNode:
def dfs(node:TreeNode)->bool:
if not node:
return False
if not dfs(node.left):
node.left=None
if not dfs(node.right):
node.right=None
return node.val==1 or node.left or node.right
if not root:
return
return root if dfs(root) else None

Leetcode 814. Binary Tree Pruning的更多相关文章

  1. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  2. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

  3. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  9. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

随机推荐

  1. Ubuntu12.04 安装adb

    1.通过apt-get安装adb sudo add-apt-repository ppa:nilarimogard/webupd8 sudo apt-get update sudo apt-get i ...

  2. Centos 7 关闭邮件服务及禁用IPv6

    关闭邮件服务(禁用25端口) sudo systemctl stop dovecot sudo systemctl stop postfix sudo systemctl disable doveco ...

  3. VS+Qt

    1.安装vs 2.安装qt[带msvc编译器的] 3.安装addin插件 4.新建qt app项目 5.在qt options里添加qt版本 路径添加到msvc那一层,如:E:\Qt5.9\5.9\m ...

  4. 如何实现Punycode中文域名转码

    如果你见过中文域名应该会觉得很奇怪,为什么复制出来的域名变成一个很莫名其妙的字符串,比如这个秀恩爱的域名“郝越.我爱你”,实际显示的域名是 http://xn--vq3al9d.xn--6qq986b ...

  5. 【eclipse】启动不了报错java was started but returned exit code=13

    原因是jdk与eclipse的版本不对,一个是32位的一个是64位的.

  6. nginx for windows 中虚拟主机路径设置问题

    由于Windows版本的Nginx其实是在Cygwin环境下编译的,所以Nginx使用的是Cygwin的路径格式,所以在Nginx的配置文件nginx.conf中,路径既不能使用*nix的格式,也不能 ...

  7. 好的Mysql 查询语句

    select swr.id,swr.name,swr.sort as type,count(swl.id) as nums,ifnull(sum(swl.package_num),0) package ...

  8. 今天 学习用到的一些知识(properties 读取,js 删除元素)

    1.properties文件位置的关系:当properties文件放在src目录下时,编译会自动把src里的文件放到bin文件平级,因此可用this.getClass.getClassLoader.g ...

  9. Object.assign() 从一个或多个源对象复制到目标对象

    Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 1.语法: Object.assign(target, ... , sources) 参 ...

  10. zabbix自动化运维学习笔记(服务器安装)

    最近博主开始接触自动化运维.首先就是zabbix这个开源的监控系统 一开始博主只是在自己的虚拟机上尝试安装.最后终于开始在公司的服务器上正式安装,教程博主也是通过度娘找的 这是原文:链接 安装环境:C ...