[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 ...
随机推荐
- 程序员的开源月刊《HelloGitHub》第61期
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 内容包括:有趣.入门级的开源项目.开源书籍.实战项目.企业级项目等,让你在短时间内感 ...
- 1027 Colors in Mars
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- leveldb的搜索
参考: http://taobaofed.org/blog/2017/07/05/leveldb-analysis/ 和leveldb源码(block.cc的Seek函数). leveldb的key. ...
- hdu5062 简单题
题意: 求区间逆序数的个数,逆序数增加了个要求就是必须要是先升序在降序例如12321或者123321这样的. 思路: 水题直接写就行了,数据范围不大,估计直接求也不会超时,我 ...
- WAF、流控设备、堡垒机
目录 WAF 流控设备 堡垒机 WAF WAF(Web Application Firewall):web防火墙,WAF是通过执行一系列针对HTTP/HTTPS的安全策略来专门对web应用提供保护的一 ...
- Msfvenonm生成一个后门木马
在前一篇文章中我讲了什么是Meterpreter,并且讲解了Meterpreter的用法.传送门-->Metasploit之Meterpreter 今天我要讲的是我们用Msfvenom制作一个木 ...
- 常见设备/CMS弱口令
目录 tomcat Apache axis2 Apache ActiveMQ zabbix RabbitMQ zentao
- MAC地址格式
随机配置一个mac地址,发现有的会报出Cannot assign requested address. 错误码是EADDRNOTAVAIL. 检查不是组播地址也不是全0地址. 组播地址就是第一个字节最 ...
- ExtJS4中Ext.onReady、Ext.define、Ext.create
1.Ext.onReady 说明:onReady内的语句块会在页面上下文加载后再执行. 2.Ext.define 说明:创建类,可以继承其他类,也可以被继承. 例子1: 1 <script ty ...
- Web端直传数据至OSS
官方文档 最佳实践 小程序直传实践 支付宝小程序直传实践 微信小程序直传实践 Web端PostObject直传实践 Web端PostObject直传实践简介 JavaScript客户端签名直传 服务端 ...