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 1 and 100 nodes.

先序遍历,找到叶子节点依次比较

/**
* 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的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. LeetCode 872. 叶子相似的树(Leaf-Similar Trees)

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

  3. LeetCode 872 Leaf-Similar Trees 解题报告

    题目要求 Consider all the leaves of a binary tree.  From left to right order, the values of those leaves ...

  4. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  5. [LeetCode] 310. Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  6. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  8. leetcode@ [310] Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  9. [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 ...

随机推荐

  1. android: LayoutInflater使用

    1. 题外话 相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个I ...

  2. UIView的层次结构–code

    转:http://blog.dongliwei.cn/archives/uiview-tree-code // Recursively travel down the view tree, incre ...

  3. 学员会诊之02:SVN协作以及Page类的设计

    三层架构的学生管理系统是我们第一个稍微大型的项目:分层.一个解决方案多个Project,所以值得我们停下来好好审查审查. 1.测试SVN服务器地址 我们的作业要求学员创建自己的SVN服务器,并且将代码 ...

  4. .NetCore中EFCore的使用整理

    EntirtyFramework框架是一个轻量级的可扩展版本的流行实体框架数据访问技术. 其中的.NetCore版本对应EntityFrameworkCore Git源代码地址:https://git ...

  5. 解决微信小程序ios端滚动卡顿的问题

    方案1:直接使用微信小程序提供的 “scroll-view " 组件. <scroll-view scroll-y style="height: 100%;"> ...

  6. linux性能采用工具oprofile使用

    1.先收藏几篇博文,先解决问题,周末继续. http://www.cnblogs.com/bangerlee/archive/2012/08/30/2659435.html http://blog.s ...

  7. ubuntu16 64位 编译64位程序和32位程序

    安装了ubuntu16 64位的系统,想在该环境下用gcc编译64位和32位的程序 默认已经安装了64位环境的gcc 1. 首先确认安装的环境是不是64位的 cocoa@cocoaUKlyn:~/De ...

  8. Swift 类型嵌套

    1.类型嵌套 Swift 支持类型嵌套,把需要嵌套的类型的定义写在被嵌套的类型的 {} 中. Swift 中的枚举类型可以辅助实现特定的类或者结构体的功能. struct SchoolUniform ...

  9. 11g新特性-SQL Plan Management

    在11g之前版本,提供了stored outlines(sql概要)特性来保存sql的执行计划. 在11g中,引入了一个新的特性sql计划管理(sql plan management)特性来保存sql ...

  10. tensorflow笔记6:tf.nn.dynamic_rnn 和 bidirectional_dynamic_rnn:的输出,output和state,以及如何作为decoder 的输入

    一.tf.nn.dynamic_rnn :函数使用和输出 官网:https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn 使用说明: A ...