题目链接

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. 叶子相似的树的更多相关文章

  1. LeetCode 872. 叶子相似的树(Leaf-Similar Trees)

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

  2. [leetcode] 872. 叶子相似的树(周赛)

    872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution { private static String ans = ""; public boolean ...

  3. 【js】Leetcode每日一题-叶子相似的树

    [js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...

  4. Leetcode872.Leaf-Similar Trees叶子相似的树

    请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树. 如果有两颗二叉树的叶值序列是相同 ...

  5. LeetCode872. 叶子相似的树

    题目 1 class Solution { 2 public: 3 vector<int>ans1; 4 vector<int>ans2; 5 bool leafSimilar ...

  6. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  7. [LeetCode] Leaf-Similar Trees 叶结点相似的树

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  8. [Swift]LeetCode872. 叶子相似的树 | Leaf-Similar Trees

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  9. [LeetCode] Same Tree 判断相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

随机推荐

  1. Cucumber 步骤中传Data Table作为参数

    引用链接:http://cukes.info/step-definitions.html Data Tables Data Tables are handy for specifying a larg ...

  2. usb-host一步一步学(二)安卓在usb-host模式下列出当前连接的usb设备

    之前写了一个简单的例子usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备,下面的这个例子是获取各种usb设备.usb接口以及usb连接点(endpoint) 正如上一 ...

  3. JavaScript随机生成颜色以及十六进制颜色 与RGB颜色值的相互转换

    /** * 随机生成颜色 * @return 随机生成的十六进制颜色 */ function randomColor(){ var colorStr=Math.floor(Math.random()* ...

  4. Django组件:用户认证组件

    一丶用户认证 1.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: (1).authent ...

  5. 前端JS电商放大镜效果

    前端JS电商放大镜效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: f ...

  7. SQL解读XML案例

    ALTER PROCEDURE [dbo].[GetProductList1] @Products XML AS BEGIN SET NOCOUNT ON DECLARE @Pointer INT D ...

  8. centos6.5_64bit-nginx安装部署

    1.配置防火墙,开启80端口.3306端口 vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dpor ...

  9. TFS看板规则

    就绪板列 准入条件 需求已完成交付 需求交付过程中的问题已全部解决 当前迭代需求所产生的BUG必须放入该列 之前迭代遗留的BUG 工作内容 需求实现概要设计 BUG确认 任务拆分 任务工作量估算(单位 ...

  10. go语言,爬取百度贴吧指定贴所有内容

    初级爬虫,为了学习一下常用的goquery. goquery 配置 go get https://github.com/PuerkitoBio/goquery 会提示不支持https方式 解决方案: ...