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 ...
随机推荐
- c++以代理的方式来实现接口化编程
假如你项目代码本身已经实现了很多的实体类,但并未采用接口,可以考虑以这种方式来实现接口化编程 struct ITest { virtual void Test()=0; }; class CTes ...
- AngularJS中使用Karma单元测试初体验
■ 搭建karma测试环境 → 创建app和test文件夹→npm install karma --save-dev→npm install karma-jasmine --save-dev→npm ...
- java判断传进来的是否是图片
public static void main(String[] args) throws IOException { String filePath = "C:\\Users\\80975 ...
- dhtmlxTreeGrid
最终效果(只添加了一级子树,可以根据需求增加级数,方法在后面). HTML和js代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Tran ...
- UVA 11613 Acme Corporation(不固定流量的最小费用流)
题意好长....变量好多.... 增加源点跟汇点.然后将每个月份看成一个点,然后拆成两个点u 跟 u+n. 从s向每个u连一条<n[u], m[i]>的弧,表示最多生产量及价值. 从每个u ...
- MySql之游标的使用
一:游标的使用场合 游标只能用于存储过程和函数中. 游标存储了检索语句的结果集,然后在存储过程和函数中可以通过游标来迭代访问结果集中的记录. 二:创建游标 CREATE PROCEDURE 存储过程名 ...
- openjudge noi 买房子
题目链接:http://noi.openjudge.cn/ch0105/16/ 总时间限制: 1000ms 内存限制: 65536kB 描述 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平 ...
- SNF快速开发平台--规则引擎在程序当中如何调用
规则定义完如何在程序当中进行使用呢? 其时很简单,只需要如下代码就可以调用程序: 规则定义: 调用代码: #region 演示2:生成左表数据(规则) POST: /api/DEMO/DemoSing ...
- nodeJs--模块module.exports与实例化方法
在nodejs中,提供了exports 和 require 两个对象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获取模块的 exports 对象.而在e ...
- Atitit 数据库表文档生成解决方案
Atitit 数据库表文档生成解决方案 1.1. Sql dml文件结构法 最快速1 1.2. Sql法+sp存储过程 (表格式样)1 1.3. Navicate uml法 (uml格式)2 1.4. ...