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. ASIHTTPRequest 详解, http 请求终结者

    转:http://www.cnblogs.com/chen1987lei/archive/2011/06/07/2074636.html ASIHTTPRequest是一款极其强劲的HTTP访问开源项 ...

  2. SQL Server 导出Excel有换行的解决方法

    参考地址::https://vcoo.cc/blog/1234/ 从 SQL Server 查询结果中复制结果数据粘贴到 Excel 中存在这么个问题:如果字段内容中有换行符,那么粘贴到 Excel ...

  3. VC++网络安全编程范例(11)-SSL高级加密网络通信(转)

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...

  4. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  5. Nginx反向代理WebSocket

    http { upstream websocket { server 192.168.1.1:8010; } server { listen 8020; location / { proxy_pass ...

  6. Excel如何固定表头,任意一行

    在日常Excel操作中,有时候内容比较多,需要将表头固定才能方便查看.那么,该如何固定表头呢?或者说如何固定任意一行我们制定的呢?下面以Excel2013进行详细的步骤讲解. 首先打开需要操作的Exc ...

  7. pureftpd.passwd解析

    将pureftpd.passwd文件的内容转换成sql语句,导入到mysql pureftp.passwd格式: <account>:<password>:<uid> ...

  8. 浏览器URL参数解决方案

    function getUrlParams() { var search = window.location.search; // 写入数据字典 , search.length).split(&quo ...

  9. golang ---tcmalloc浅析

    总体结构 在tcmalloc内存管理的体系之中,一共有三个层次:ThreadCache.CentralCache.PageHeap,如上图所示.分配内存和释放内存的时候都是按从前到后的顺序,在各个层次 ...

  10. MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)

    继昨晚之后.决定更新系统自带的svn.自带的svn版本号是1.7.看官网svn:http://www.wandisco.com/subversion/download#osx 最新版本号是1.9.13 ...