[leetcode] 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. 叶子相似的树(周赛)的更多相关文章
- LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树 872. Leaf-Similar Trees 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列. LeetCode872. Leaf- ...
- 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")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- [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 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
- [LeetCode] 100. Same Tree 相同树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
随机推荐
- OO博客总结——OO落下帷幕
OO博客总结--OO落下帷幕 凡此过往,皆为序章. 不知不觉OO课程即将落下帷幕,一路坎坎坷坷磕磕绊绊,可算是要结束了,心里终于松了一口气,也有小小的不甘和遗憾.凡此过往,皆为序章.特殊的线上OO课程 ...
- Linux下查看CPU、内存占用率
linux下查看最消耗CPU.内存的进程 CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 内存消耗最多的前10个进程: ps ...
- CentOS7用yum安装软件提示 cannot find a valid baseurl for repobase7x86_64
解决办法[亲测有效] 1.打开 vi /etc/sysconfig/network-scripts/ifcfg-enp4s0(每个机子都可能不一样,但格式会是"ifcfg-e..." ...
- net -snmp 的监控策略
yum install net-snmp net-snmp-utils -y vim /etc/snmp/snmpd.conf 最后添加:rocommunity nmap 192.168.1.0/24 ...
- js中return;、return true、return false;区别
一.返回控制与函数结果 语法为:return 表达式 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制 无函数结果,语法为:return; 在大多数情况下,为事件处理函数返 ...
- [CTF]栅栏密码
[CTF]栅栏密码 --------------------- 作者:adversity` 来源:CSDN 原文:https://blog.csdn.net/qq_40836553/articl ...
- Compare the contents of two arrays
✍️Define a methed to compare the contents of two arrays and return the result . 定义一个方法,用于比较两个数组的内容是否 ...
- 从0开始fastjson漏洞分析
关于fastjson漏洞利用参考:https://www.cnblogs.com/piaomiaohongchen/p/10799466.html fastjson这个漏洞出来了很久,一直没时间分析, ...
- 【敏杰开发】Beta阶段事后分析
[敏杰开发]Beta阶段事后分析 设想和目标 Q 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们达到目标了么(原计划的功能做到了几个? 按照原计划交付时间交付 ...
- 面试遇到的坑JS深拷贝和浅拷贝
首先要搞明白深拷贝和钱拷贝的区别要先搞明白 栈和堆的区别 一.栈 栈存储基础数据类型,如: String.Number.Boolean.Null.Underined,这些简单的基础数据类型能够直接存储 ...