LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树
872. Leaf-Similar Trees
题目描述
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列。

LeetCode872. Leaf-Similar Trees简单
举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false。
提示:
- 给定的两颗树可能会有 1 到 100 个结点。
Java 实现
TreeNode Class
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
if (root1 == null || root2 == null) {
return root1 == null && root2 == null ? true : false;
}
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
preorder(root1, sb1);
preorder(root2, sb2);
return sb1.toString().equals(sb2.toString());
}
public void preorder(TreeNode root, StringBuffer sb) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
sb.append(root.val);
}
preorder(root.left, sb);
preorder(root.right, sb);
}
}
参考资料
- https://leetcode.com/problems/leaf-similar-trees/
- https://leetcode-cn.com/problems/leaf-similar-trees/
LeetCode 872. 叶子相似的树(Leaf-Similar Trees)的更多相关文章
- [leetcode] 872. 叶子相似的树(周赛)
872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution { private static String ans = ""; public boolean ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 表达式树(Expression Trees)
[翻译]表达式树(Expression Trees) 原文地址:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/con ...
- [Swift]LeetCode872. 叶子相似的树 | Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- LeetCode.872-叶子值相等的树(Leaf-Similar Trees)
这是悦乐书的第334次更新,第358篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第204题(顺位题号是872).考虑二叉树的所有叶子,从左到右的顺序,这些叶子的值形成叶 ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
随机推荐
- 定时任务 Scheduled quartz
在项目应用中往往会用到任务定时器的功能,比如某某时间,或者多少多少秒然后执行某个骚操作等.spring 支持多种定时任务的实现,其中不乏自身提供的定时器.接下来介绍一下使用 spring 的定时器和使 ...
- 002——keil-Error: L6915E: Library reports error: __use_no_semihosting was requested解决
..\OBJ\KEY.axf: Error: L6915E: Library reports error: __use_no_semihosting was requested, but _ttywr ...
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
1.添加jackson库 如果是maven工程,需要在pom.xml中添加jackson的依赖: <dependency> <groupId>com.fasterxm ...
- know thself
- UFUN函数 UF_CFI函数(uc4504,uc4540,uc4514,uc4547,UF_CFI_ask_file_exist )
UF_initialize(); //指定本地数据文件的路径 char file_spec[]="D://Program Files//Siemens//NX 8.0//UGII//zyTO ...
- centos7中运行ifconfig提示-bash: ifconfig: command not found
centos7中运行ifconfig提示-bash: ifconfig: command not found 查看/sbin/下是否有ifconfig,若没有通过如下命令安装 sudo yum ins ...
- GoCN每日新闻(2019-10-25)
GoCN每日新闻(2019-10-25) GoCN每日新闻(2019-10-25) 1. [译]Golang应付百万级请求/分钟 https://juejin.im/post/5db1464b6fb9 ...
- mybatis 获取新增数据的主键
<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys= ...
- mysql addtime() 函数
mysql> select addtime('1997-12-31 23:59:52' , '1 1:1:1'); +-------------------------------------- ...
- manjaro系统的回滚操作
作为linux系统的爱好者,自从使用linux后,就喜欢追求新的软件,连系统都换成了滚动升级的版本.manjaro基于arch linux,同时也是kde的支持系统,升级非常频繁.使用了几年,很少碰到 ...