LeetCode 250. Count Univalue Subtrees - 二叉树系列题23

这篇博客介绍了如何解决LeetCode中的250题,即给定一棵二叉树,找出所有节点值相同的子树,并返回其数量。通过分析节点的四种情况(无子树、只有一个子树、有两个子树),博主提出利用二叉树的后序遍历来判断每个节点及其子树是否构成单一值子树,并统计这些子树的个数。
摘要由CSDN通过智能技术生成

Given the root of a binary tree, return the number of uni-value subtrees.

A uni-value subtree means all nodes of the subtree have the same value.

Example 1:

Input: root = [5,1,5,5,5,null,5]
Output: 4

Example 2:

Input: root = []
Output: 0

Example 3:

Input: root = [5,5,5,5,5,null,5]
Output: 6

Constraints:

  • The number of the node in the tree will be in the range [0, 1000].
  • -1000 <= Node.val <= 1000

题目给定一棵二叉树,要求找出所有具有单一节点值的子树(即子树中所有节点值都相等),返回单一值子

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值