872. 叶子相似的树

前序遍历,记录叶子节点即可

class Solution {
private static String ans = ""; public boolean leafSimilar(TreeNode root1, TreeNode root2) {
ans = "";
String ans1 = "", ans2 = "";
fun(root1);
ans1 = ans;
ans = "";
fun(root2);
ans2 = ans;
return ans1.equals(ans2);
} public void fun(TreeNode treeNode) {
if (treeNode.left == null && treeNode.right == null) {
ans += treeNode.val;
}
if (treeNode.left != null) {
fun(treeNode.left);
}
if (treeNode.right != null) {
fun(treeNode.right);
}
}
}

[leetcode] 872. 叶子相似的树(周赛)的更多相关文章

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

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

  2. Leetcode 872. 叶子相似的树

    题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...

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

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

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

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

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

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

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

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

  7. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  8. LeetCode 刷题笔记 (树)

    1.  minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...

  9. [LeetCode] 100. Same Tree 相同树

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

随机推荐

  1. Word 查找和替换字符串方法

    因为项目需要通过word模板替换字符串 ,来让用户下载word, 就在网上找了找word查找替换字符串的库或方法,基本上不是收费,就是无实现,或者方法局限性太大 .docx 是通过xml来存储文字和其 ...

  2. JPA简单的分页条件查询

    1,service层代码: @Override public QrCodeRecordPaging getPage(String projectId, Integer pageNumber, Inte ...

  3. mooc人大单元测试2

    1 单选(2分) 下列选项中不是关系数据库基本特征的是(  ). A. 不同的列应有不同的数据类型 B. 不同的列应有不同的列名 C. 与行的次序无关 D. 与列的次序无关 2 单选(2分) 关系代数 ...

  4. 1038 Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  5. 通过中转DLL函数实现DLL劫持

    当我们运行程序时,一般情况下会默认加载Ntdll.dll和Kernel32.dll这两个链接库,在进程未被创建之前Ntdll.dll库就被默认加载了,三环下任何对其劫持都是无效的,除了该Dll外,其他 ...

  6. Linux系统登录相关

    whoami:查看当前用户 who:查看当前登录系统的所有用户 tty指的是主机的图形化界面的面板 pts/x指的是远程ssh连接的窗口 who -b:主机的上一次启动时间 w:显示已经登陆系统的用户 ...

  7. 19.Vuex详细使用说明-一篇文章涵盖所有知识点

    vuex官网: https://vuex.vuejs.org/zh/ 一. 前言 不管是Vue,还是 React,都需要管理状态(state),比如组件之间都有共享状态的需要. 什么是共享状态? 比如 ...

  8. Linux安装与使用FTP服务-vsftpd

    简介 vsftpd 是"very secure FTP daemon"的缩写,安全性是它的一个最大的特点.vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行 ...

  9. 最近做app项目中遇到的问题 以及一些常见注意事项

    最近做app项目中遇到的问题 1.时间兼容问题 var date = "2019-12-18 18:03:45" //不兼容代码 var newDate = new Date(da ...

  10. @JsonFormat 格式化时间 时出现时间不准确问题

    今天突然报个问题,简单来说说就是数据库某一字段的记录时间为 14点,然而展示到前台却是 6点 我腚眼一看,postman测试的数据也是6点 然而idea查出来的并不是6点 再仔细一瞅idea实体类的时 ...