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. Vim 基本設置 – 使用Vim-plug管理插件 (3)【转】

    本文转载自:https://staryoru.github.io/vim-plugin-manager/ Vim中有很多非常好用的插件(plugin),對於這些插件的安裝.更新與移除等等,使用一個插件 ...

  2. Java数字证书操作

    为服务器生成证书 keytool -genkey -v -alias tomcat -keyalg RSA -keystore D:\tomcat.keystore -validity 36500 为 ...

  3. C# 删除文件以及文件夹

    代码如下: /// <summary> /// 删除文件夹以及文件 /// </summary> /// <param name="directoryPath& ...

  4. MySQL 存储过程参数用法 in, out, inout

    MySQL 存储过程参数有三种类型:in.out.inout.它们各有什么作用和特点呢? 一.MySQL 存储过程参数(in) MySQL 存储过程 “in” 参数:跟 C 语言的函数参数的值传递类似 ...

  5. java工具类使用

    ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "C ...

  6. ElasticSearch介绍与安装

    什么是ES? 1基于Apache Lucene构建的开源搜索引擎 2采用java编写,提供简单易用的RESTFul API 3轻松的横向扩展,可支持PB级的结构化或非结构化数据处理 ES的应用场景? ...

  7. SEA 教程

    Sina App Engine(SAE)教程(11)- Yaf使用 Sina App Engine(SAE)入门教程(10)- Cron(定时任务)使用 Sina App Engine(SAE)入门教 ...

  8. shell数组中“和@的妙用

    #!/bin/bashlist=(4k"8k a bit""16k abc""32k gold"64k)for i in "${l ...

  9. wireshark初学者使用

    介绍 Wireshark是一款网络封包分析软件,截取网络封包,显示其封包的详细信息.日常工作中用的比较多.在使用wireshark之前须了解常用的网络协议.如:tcp,http,ip,udp等.(其实 ...

  10. Project facet Java 1.8 is not supported by target runtime Apache Tomcat v7.0.

    找到项目下的“.settings”