这是悦乐书的第366次更新,第394篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第228题(顺位题号是965)。如果树中的每个节点具有相同的值,则二叉树是单一的。当且仅当给定树是单一时才返回true

    1
/ \
1 1
/ \ \
1 1 1

输入: [1,1,1,1,1,null,1]

输出: true

    2
/ \
2 2
/ \
5 2

输入: [2,2,2,5,2]

输出: false

注意

  • 给定树中的节点数量将在[1,100]范围内。

  • 每个节点的值将是[0,99]范围内的整数。

02 第一种解法

题目的意思是判断二叉树中的节点值是否都是一个值,为同一个值就返回true,不是就返回false

思路:使用递归,中序遍历二叉树的每个节点,存入List中,再遍历比较List中的元素是否都等于二叉树的根节点值。

public boolean isUnivalTree(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
helper(root, list);
for (Integer num : list) {
if (num != root.val) {
return false;
}
}
return true;
} public void helper(TreeNode root, List<Integer> list) {
if (root == null) {
return ;
}
helper(root.left, list);
list.add(root.val);
helper(root.right, list);
}

03 第二种解法

针对第一种解法的递归方式,我们也可以换成迭代的方式,借助栈Stack来实现。

public boolean isUnivalTree2(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
list.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
for (Integer num : list) {
if (num != root.val) {
return false;
}
}
return true;
}

04 第三种解法

在第二种解法的基础上,我们可以直接判断出栈的树的节点值是否等于根节点值,省掉存入List的步骤。

public boolean isUnivalTree3(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.val != root.val) {
return false;
}
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
return true;
}

05 第四种解法

既然判断节点值是否都是同一个值,那么可以借助HashSet去重的特性,使用递归,中序遍历节点值,存入HashSet中,最后判断HashSetsize是否等于1即可。

public boolean isUnivalTree4(TreeNode root) {
Set<Integer> set = new HashSet<Integer>();
helper(root, set);
return set.size() == 1;
} public void helper(TreeNode root, Set<Integer> set) {
if (root == null) {
return ;
}
helper(root.left, set);
set.add(root.val);
helper(root.right, set);
}

06 第五种解法

针对第四种解法,也可以通过迭代的方式的来实现,借助栈Stack

public boolean isUnivalTree5(TreeNode root) {
Set<Integer> set = new HashSet<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
set.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
return set.size() == 1;
}

07 第六种解法

我们也可以直接用递归,不借助其他的类。

public boolean isUnivalTree6(TreeNode root) {
return help(root, root.val);
} public boolean help(TreeNode root, int num){
if (root != null && root.left == null
&& root.right == null && root.val == num) {
return true;
}
if (root != null && root.left == null) {
return root.val == num && help(root.right, num);
}
if (root != null && root.right == null) {
return root.val == num && help(root.left, num);
}
return root != null && root.val == num
&& help(root.right, num) && help(root.left, num);
}

08 第七种解法

针对上面的第六种解法,我们还可以再简化下。因为题目给了二叉树节点的数量范围,root是不会为空的,等于null表示当前没有继续可以向下遍历的节点了。

public boolean isUnivalTree7(TreeNode root) {
return helper(root, root.val);
} public boolean helper(TreeNode root, int num){
if (root == null) {
return true;
}
if (root.val != num) {
return false;
}
return helper(root.right, num) && helper(root.left, num);
}

09 小结

算法专题目前已连续日更超过七个月,算法题文章234+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.965-单一二叉树(Univalued Binary Tree)的更多相关文章

  1. [Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

    A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 965. Univalued Binary Tree

    题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  6. 【Leetcode_easy】965. Univalued Binary Tree

    problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  9. 数据结构-二叉树(Binary Tree)

    1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成.  2.特数二 ...

随机推荐

  1. 分布式中 CAP BASE ACID 理解(转载)

    概念理解(CAP,BASE, ACID) CAP CAP:  Consistency, Availability, Partition-tolerance 强一致性(Consistency).系统在执 ...

  2. hdu3586 Information Disturbing[二分答案+树形DP]

    给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足 ...

  3. 【洛谷P1280】尼克的任务

    题目大意:一个人在时间 [1,N] 内工作,现有 M 个任务,每个任务需要在一段固定的时间区间内完成,任务之间的时间可能有重叠.若当前时间有任务要开始,且人处于空闲状态,则一定要这个人来做,否则这个人 ...

  4. buuctf@pwn1_sctf_2016

    from pwn import * sh=remote('pwn.buuoj.cn',20086) get_flag=0x08048F0D payload='I'*0x14+'a'*4+p32(get ...

  5. C# 选择文件夹 选择文件

    选择文件 //选择文件 OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 ...

  6. 窗体操作:CBrush类

    CBrush画刷定义了一种位图形式的像素,利用它可对区域内部填充颜色. 该类封装了Windows的图形设备接口(GDI)刷子.通过该类构造的CBrush对象可以传递给任何一个需要画刷的CDC成员函数. ...

  7. CF1213D Equalizing by Division

    easy version hard version 问题分析 直接从hard version入手.不难发现从一个数\(x\)能得到的数个数是\(O(\log x)\)的.这样总共有\(O(n\log ...

  8. 我的 CSDN 博客目录索引(主要记录了我学习视频、书籍的笔记,持续更新中)

    我的 CSDN 博客地址: lw_power的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/lw_power 佟刚老师<Spring4视频教程>学习笔记 ...

  9. 整合spring之后,struts2里面的自定义拦截器的invocation.invoke()总是返回input

    这个真的是整死我了,还好看见了一篇博客提示了我, 解决方法: 在spring的bean配置中我没有设置action的作用域为prototype,也就是多例的,如果不设置则就会是默认的singleton ...

  10. C++入门经典-例3.11-使用if语句来实现根据输入的字符输出字符串

    1:代码如下: // 3.11.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...