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

举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
提示:
- 给定的两颗树可能会有 1 到 100 个结点。
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> v1;
vector<int> v2;
Get(root1, v1);
Get(root2, v2);
if(v1.size() != v2.size())
return false;
for(int i = 0; i < v1.size(); i++)
{
if(v1[i] != v2[i])
return false;
}
return true;
}
void Get(TreeNode* root, vector<int> &v)
{
if(root == NULL)
return ;
if(root ->left == NULL && root ->right == NULL)
v.push_back(root ->val);
if(root ->left != NULL)
{
Get(root ->left, v);
}
if(root ->right != NULL)
{
Get(root ->right,v);
}
}
};
Leetcode872.Leaf-Similar Trees叶子相似的树的更多相关文章
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- LeetCode872. 叶子相似的树
题目 1 class Solution { 2 public: 3 vector<int>ans1; 4 vector<int>ans2; 5 bool leafSimilar ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树 872. Leaf-Similar Trees 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列. LeetCode872. Leaf- ...
- [leetcode] 872. 叶子相似的树(周赛)
872. 叶子相似的树 前序遍历,记录叶子节点即可 class Solution { private static String ans = ""; public boolean ...
- [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] Leaf-Similar Trees 叶结点相似的树
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- Spark Gradient-boosted trees (GBTs)梯度提升树
梯度提升树(GBT)是决策树的集合. GBT迭代地训练决策树以便使损失函数最小化. spark.ml实现支持GBT用于二进制分类和回归,可以使用连续和分类特征. 导入包 import org.apac ...
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
随机推荐
- 查看MySql版本号命令
转自:https://blog.csdn.net/qq_38486203/article/details/80324014 这里介绍四中不同的方法,它们分别运行在不同的环境中,最后对每种方法的优劣以 ...
- 分批次删除大表数据的shell脚本
#!/bin/bash # 分别是主机名,端口,用户,密码,数据库,表名称,字段名称 readonly HOST="XXX" readonly PORT=" readon ...
- leetcode 376Wiggle Subsequence
用dp解 1)up定义为nums[i-1] < nums[i] down nums[i-1] > nums[i] 两个dp数组, up[i],记录包含nums[i]且nums[i-1] & ...
- day 71作业
作业: url配置 urlpatterns = [ url(r'^v2/cars/$',views.CarAPIView.as_view()), url(r'^v2/cars/(?P<pk> ...
- ArcMap中给点shp添加字段后,shp文件破坏无法打开
这两天遇到一个奇怪的问题,在整理项目中的建筑物数据时发现,有几个图层进行字段添加后出现问题,shp文件被损坏了.这问题很隐蔽,给shp添加字段后不报错,进行赋值,报错如下: 但是无论是选择“是”还是“ ...
- 微端 打包更新工具 as air 分享
分享 微端,更新的是散包,不像端游,一个大包搞定. 更新须要每次用工具把资源的散文件.依据文件夹结构及时间 生成一个列表, 每次更新就是 文件夹及时间的比对! 该project能够翻译成 其它语言.有 ...
- 使用C#反射实现用户控件调用父页面方法
using System.Reflection; MethodInfo mi = this.Page.GetType().GetMethod("GetUserName"); //该 ...
- 2019-1-29-Roslyn-使用-WriteLinesToFile-解决参数过长无法传入
title author date CreateTime categories Roslyn 使用 WriteLinesToFile 解决参数过长无法传入 lindexi 2019-01-29 16: ...
- ubuntu 16.04 换源
第一步:备份原来的源文件 cd /etc/apt/ 命令 cp sources.list sources.list.bak 将sources.list备份到sources.list.bak 第二步:替 ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它 ...