题目链接

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. 手机uc浏览器,获取到图片,但左上有小图标的问题

    手机uc浏览器有个坑 获取不到图片,左上是这样的

  2. Swift网络库Alamofire的导入

    一.手动导入 1, 官网下载 Alamofire 2, 解压下载的文件 放入工程的顶层目录下 3, 打开工程 Add Files 4, 选中项目 TARGETS > General > E ...

  3. oracle报错:ORA-01658(转自52斋347)

    在oracle里创建表,报出错:ORA-01658: 无法为表空间space中的段创建 INITIAL 区:或者: ORA-01658: unable to create INITIAL extent ...

  4. python之其他模块的用法

    1.时间模块   在Python中通常有三种表示时间的方式,分别是时间戳.元组.格式化的时间字符串. 时间模块的常用方法 time.sleep() #指定延迟时间 time.time() #当前时间的 ...

  5. 《C++面向对象程序设计》之变量的生存期

    <C++面向对象程序设计>之变量的生存期 静态生存期 (1)全局静态生存期:在函数体外声明,作用域从声明处开始到文件结尾处结束,称其有文件作用域,相当于全局变量 . (2)局部静态生存期: ...

  6. C++ list类详解

    转自:http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素 ...

  7. iphone 弹出键盘,文本框自动向上移动。

    1.让类继承UITextViewDelegate UITextView *inputTextView;UIScrollView * _scrollView; 2.在init函数中先创建scrollVi ...

  8. selenium 使用键盘时 提示java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.

    输入某个内容后,使用enter键进行确认,最开始使用方式为: driver.findElement(By.xpath("//input[@name='supplier_name'][@id= ...

  9. fifo - 先进先出的特殊文件, 又名管道

    描述 (DESCRIPTION) 一个 FIFO 特殊 文件 (又名 管道) 同 管道线 相似, 但是 它是 作为 文件 系统 的一部分 访问的. 可以 有 多个 进程 打开它 以供 读写. 当 进程 ...

  10. python_10_for guess

    age_of_oldboy=56 count=0 for count in range(3): guess_age=int(input('guess age:')) if guess_age==age ...