Leetcode 872. 叶子相似的树
题目链接
https://leetcode-cn.com/problems/leaf-similar-trees/description/
题目描述
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。

举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
提示:
- 给定的两颗树可能会有 1 到 100 个结点。
题解
把每棵树的叶子节点存起来,在进行比较。
代码
/**
* 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) {
if ((root1 != null && root2 != null) || (root1 == null && root2 == null)) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
findLeaf(root1, list1);
findLeaf(root2, list2);
if (list1.size() != list2.size()) { return false; }
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i) != list2.get(i)) { return false; }
}
return true;
}
return false;
}
public void findLeaf(TreeNode root, List<Integer> list) {
if (root == null) { return; }
if (root.left == null && root.right == null) { list.add(root.val); }
findLeaf(root.left, list);
findLeaf(root.right, list);
}
}
Leetcode 872. 叶子相似的树的更多相关文章
- LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树 872. Leaf-Similar Trees 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列. LeetCode872. Leaf- ...
- [leetcode] 872. 叶子相似的树(周赛)
872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution { private static String ans = ""; public boolean ...
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- Leetcode872.Leaf-Similar Trees叶子相似的树
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两颗二叉树的叶值序列是相同 ...
- LeetCode872. 叶子相似的树
题目 1 class Solution { 2 public: 3 vector<int>ans1; 4 vector<int>ans2; 5 bool leafSimilar ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- [LeetCode] Leaf-Similar Trees 叶结点相似的树
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- [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] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
随机推荐
- elasticsearch报错:None of the configured nodes are available: []
问题:在内网测试的时候可以正常访问,但是部署到外网上客户端连接elasticsearch报错:None of the configured nodes are available: [] 原因:默认情 ...
- magento新增商品属性以及将属性加入Flat table
magento的EAV模型非常强大且灵活,但是如果不做优化的话,性能会非常低,因为attributes都存放在附表里,要获取一个entity的attribute,需要表联结一次,如果需要获取多条att ...
- 学习《CSS选择器Level-4》不完全版
1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...
- python小游戏之贪吃蛇
本程序需要安装pygame,请自行百度安装...... 废话不多说,直接上代码 import pygame,sys,time,random from pygame.locals import * # ...
- Eucalyptus学习汇总
Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) 是一种开 ...
- IntelliJ IDEA IDEA 2018 激活注册码
K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...
- 微信jssdk实现分享到微信
本来用的是这个插件http://overtrue.me/share.js/和百度分享 相同之处:在微信分享的时候,分享的链接都不能获取到缩略图... 不同之处:百度分享在微信低版本是可以看到缩略图的( ...
- HDU5171 矩阵快速幂
题目描述:http://acm.hdu.edu.cn/showproblem.php?pid=5171 算法: 可以先将数组a[]排序,然后序列 a1 , a2 , … , an 即为有序序列,则第一 ...
- StringBuffer是可变的还是不可变的?
前言:我们知道String类的修饰符是final,其char[] value也是由final修饰的,每次给String变量赋一个新值,都会创建一个新的String对象,很多有涉及到字符串本身的改变都是 ...
- ShopNC B2B2C多用户商城网站系统源码
直接正常安装就行哦 注意有服务器的安装可以更下安装时间的长度 也就是说进行跳转的 如果时间太少 这样会安装不成 数据导入不完成 所以就会安装不成功安装好后打开data\config\config.in ...