LeetCode.872-叶子值相等的树(Leaf-Similar Trees)
这是悦乐书的第334次更新,第358篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第204题(顺位题号是872)。考虑二叉树的所有叶子,从左到右的顺序,这些叶子的值形成叶子值序列。
例如,在上面给定的树中,叶子值序列是(6,7,4,9,8)。
如果两个二叉树的叶子值序列相同,则认为两个二叉树是叶子相似的。当且仅当具有头节点root1和root2的两个给定树是叶类似的时,才返回true。
注意:
- 两个给定的树将具有1到100个节点。
本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。
02 第一种解法
题目的意思是给定两个二叉树,如果它们的叶子值都相等,就返回true。
也就是说,我们需要拿到二叉树的所有叶子节点的值,然后依次比较,如果相等就返回true。
从树的结构上来看,使用广度遍历的方式更好,整体思路就是使用广度优先算法,得到叶子节点值数组,再比较数组值判断是否相等。
此解法是借助栈,通过迭代的方式来实现广度优先算法。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> list = getLeafValue(root1);
List<Integer> list2 = getLeafValue(root2);
if (list.size() != list2.size()) {
return false;
}
for (int i=0; i<list.size(); i++) {
if (list.get(i) != list2.get(i)) {
return false;
}
}
return true;
}
public List getLeafValue(TreeNode root){
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
int size = stack.size();
for (int i=0; i<size; i++) {
TreeNode node = stack.pop();
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
if (node.left == null && node.right == null) {
list.add(node.val);
}
}
}
return list;
}
}
03 第二种解法
针对广度优先算法,我们也可以使用递归的方式来实现。
递归方法中使用了两个参数,一是二叉树本身,二是存储叶子节点值的数组。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> list = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
getLeafValue(root1, list);
getLeafValue(root2, list2);
if (list.size() != list2.size()) {
return false;
}
for (int i=0; i<list.size(); i++) {
if (list.get(i) != list2.get(i)) {
return false;
}
}
return true;
}
public void getLeafValue(TreeNode root, List<Integer> list){
if (root != null) {
if (root.left == null && root.right == null) {
list.add(root.val);
}
getLeafValue(root.left, list);
getLeafValue(root.right, list);
}
}
}
04 第三种解法
在第二种解法的基础上,我们可以将数组换成字符串,思路和效果都是一样的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
getLeafValue(root1, sb);
getLeafValue(root2, sb2);
return sb.toString().equals(sb2.toString());
}
public void getLeafValue(TreeNode root, StringBuilder sb){
if (root != null) {
if (root.left == null && root.right == null) {
sb.append(root.val);
}
getLeafValue(root.left, sb);
getLeafValue(root.right, sb);
}
}
}
05 小结
算法专题目前已连续日更超过六个月,算法题文章204+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.872-叶子值相等的树(Leaf-Similar Trees)的更多相关文章
- LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树 872. Leaf-Similar Trees 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列. LeetCode872. Leaf- ...
- [leetcode] 872. 叶子相似的树(周赛)
872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution { private static String ans = ""; public boolean ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- 【bzoj3685】普通van Emde Boas树 权值zkw线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6809743.html 题目描述 设计数据结构支持:1 x 若x不存在,插入x2 x 若x存在,删除x3 输 ...
- 表达式树(Expression Trees)
[翻译]表达式树(Expression Trees) 原文地址:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/con ...
- Java实现 LeetCode 677 键值映射(字典树)
677. 键值映射 实现一个 MapSum 类里的两个方法,insert 和 sum. 对于方法 insert,你将得到一对(字符串,整数)的键值对.字符串表示键,整数表示值.如果键已经存在,那么原来 ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【leetcode】988. Smallest String Starting From Leaf
题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- SpringBoot之模板引擎
一.Thymeleaf 1.1 集成 templates 在 pom.xml 文件中添加依赖 <dependencies> ...... <dependency> <gr ...
- percona 5.7 + tokudb
percona 5.7 + tokudb Percona + TokuDB 部署 # 参考资料https://www.kancloud.cn/devops-centos/centos-linux-de ...
- Docker(六):Dockerfile命令详解
Dockerfile 指令详解 1 FROM 指定基础镜像 FROM 指令用于指定其后构建新镜像所使用的基础镜像.FROM 指令必是 Dockerfile 文件中的首条命令,启动构建流程后,Docke ...
- 【NOIP2012模拟10.25】旅行
题目 给定一个n行m列的字符矩阵,'.'代表空地,'X'代表障碍.移动的规则是:每秒钟以上下左右四个方向之一移动一格,不能进入障碍. 计算:在空地中随机选择起点和终点(可以重合,此时最短耗时为0),从 ...
- 2019春Python程序设计作业1(0319-0325)
判断题 1-1 在Python 3.x中可以使用中文作为变量名. (2分) T F Python变量使用前必须先声明,并且一旦声明就不能再当前作用域内改变其类型.(2分) T ...
- HTTS TTLS 433
HTTP和HTTPS协议,看一篇就够了 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/x ...
- layui 源码解读(部分)
<!DOCTYPE html> <head> </head> <body> <input type="button" id=& ...
- JPA学习(四、JPA_映射关联关系)
框架学习之JPA(四) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...
- POJ 2155 Matrix (树状数组 && 区间计数)
题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...
- 更好的构建 Node 服务的工具
更好的构建 Node 服务的工具 无论前端项目在打包后都发送给后台, 有时候自己想看看效果在运行 npm run build 后只是看到一个 build 文件夹,但是直接打开是无法浏览,因此需要开启一 ...