LeetCode - 872. Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).
Two binary trees are considered leaf-similar if their leaf value sequence is the same.
Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
Note:
- Both of the given trees will have between
1and100nodes.
先序遍历,找到叶子节点依次比较
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<TreeNode> nodes1 = new ArrayList<>();
private List<TreeNode> nodes2 = new ArrayList<>();
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
frontTraversal(root1, nodes1);
frontTraversal(root2, nodes2);
int len1 = nodes1.size();
int len2 = nodes2.size();
if (len1 != len2)
return false;
for (int i=0; i<len1; i++) {
if (nodes1.get(i).val != nodes2.get(i).val)
return false;
}
return true;
} public void frontTraversal(TreeNode root, List<TreeNode> nodes) {
if (root == null)
return;
if (root.left != null)
frontTraversal(root.left, nodes);
if (root.left == null && root.right == null)
nodes.add(root);
if (root.right != null)
frontTraversal(root.right, nodes); }
}
LeetCode - 872. Leaf-Similar Trees的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode 872. 叶子相似的树(Leaf-Similar Trees)
872. 叶子相似的树 872. Leaf-Similar Trees 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个叶值序列. LeetCode872. Leaf- ...
- LeetCode 872 Leaf-Similar Trees 解题报告
题目要求 Consider all the leaves of a binary tree. From left to right order, the values of those leaves ...
- [LeetCode] 310. Minimum Height Trees 解题思路
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] 310. Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- leetcode@ [310] Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点
Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...
随机推荐
- [数据结构与算法分析(Mark Allen Weiss)]二叉树的插入与删除 @ Python
二叉树的插入与删除,来自Mark Allen Weiss的<数据结构与算法分析>. # Definition for a binary tree node class TreeNode: ...
- CentOS 7安装SSHFS 实现远程主机目录 挂载为本地目录
安装sshfs 官方下载地址 https://github.com/libfuse/sshfs/releases 首先,我们需要安装sshfs软件.sshfs是一个基于SSH文件传输协议的文件系统客户 ...
- yum离线安装rpm包
CentOS利用yum下载好rpm包,并离线安装 1.联网安装好rpm包,并将下载好的包备好 #yum install --downloadonly --downloaddir=/home/sam ...
- 理解 .NET 2015
去年跟着BUILD之后,我发了一篇文章Exciting Times for .NET 并从那以后我已经很荣幸地能够与.NET团队并肩作战,这其中包括了运行时.框架.语言和编译器.虽然去年我的重心已经更 ...
- Global Mapper如何加载在线地图
Global Mapper是一个比较好用的GIS数据处理软件,官网:http://www.bluemarblegeo.com/products/global-mapper.php ,除使用ArcGIS ...
- IntelliJ IDEA 2017.3/2018.1激活与汉化
本文以IntelliJ IDEA 2017.3以及2018.1为例进行讲解.(持续更新:2018年5月28日) 适用版本(其它版本未测试): IntelliJ IDEA 2017.2.6.2017.3 ...
- iOS 7设计备忘单
With the release of iOS 7, app designers and developers will need to adjust their visual language to ...
- top命令详析及排查问题使用演示
1. top基本使用 top命令运行图 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 buff/cache: buffers 和 cache 都是内存中存放的数 ...
- SNF快速开发平台3.0之--系统里广播的作用--迅速及时、简明扼要的把信息发送给接收者
广播信息,即速度快捷.迅速及时.简明扼要的把信息发送给接收者. 当然在SNF快速开发平台上你也可以作为公告使用.不管当做什么使用要满足以下需求: 简单操作:页面操作简单 只需要输入内容就可以发送. 灵 ...
- MySQL四种事务隔离级别详解
本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...