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 ...
随机推荐
- 纯js房贷计算器开源
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php+tcpdf生成pdf: 中文乱码
TCPDF是一个生成PDF的不错的库,可惜,官方对包括中文在内的东亚字体支持不怎么样的.场景:某项目需要根据数据库信息生成pdf格式的发票,考虑采用稳定的tcpdf,虽然还有许多其它选择,但是这个应该 ...
- SQL Server 父子迭代查询语句,树状查询
这个也有用: -- Get childs by parent idWITH TreeAS( SELECT Id,ParentId FROM dbo.Node P WHERE P.Id = 21 -- ...
- MySQL高级管理
#mysql数据库密码的修改 一.mysql密码的修改与恢复 1.修改密码 mysqladmin -u root -p123 password 456 数据库内修改 method.first: upd ...
- Kali Linux 2019.4中文乱码解决
1.先换源deb http://mirrors.aliyun.com/kali kali-rolling main non-free contribdeb-src http://mirrors.ali ...
- webuploader之大文件分段上传、断点续传
文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...
- 开源项目 08 IOC Autofac
using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 【后缀数组】【LuoguP4248】 [AHOI2013]差异
题目链接 题目描述 给定一个长度为 n 的字符串 S,令 Ti 表示它从第 i 个字符开始的后缀.求 \(\sum_{1\le i <j\le n}len(T_i)+len(T_j)-2*lcp ...
- 【POJ1573】Robot Motion
题目传送门 本题知识点:模拟 本题的题意也很简单. 给出一个矩阵,矩阵里面有着东南西北(上下左右)的指示,当机器人走到上面时则会按照指示前进.机器人每次都从最上面一行的某一列进入. 需要判断的是机器人 ...
- socket http tcp ip 区别联系
功能是实现继承复用.刚才做了一个简要的概述,里面有一些常用的概念,这里做个简短的概念普及介绍:(1),TCP/IP------TPC/IP协议是传输层协议,主要解决数据如何在网络中传输.(2),Soc ...