题目地址:

https://www.lintcode.com/problem/leaf-similar-trees/description

定义一棵二叉树的”树叶序列“,为其所有叶子从左到右排列所成的序列。两个叶子 x x x和 y y y, x x x排在 y y y前面当且仅当 x x x在其最近公共祖先 l c a ( x , y ) lca(x,y) lca(x,y)的左子树中并且 y y y在右子树中,或者其一为树根然后另一个在左或右子树中。

思路是,先用DFS求出两棵树的叶子序列,最后做比较。代码如下:

import java.util.ArrayList;
import java.util.List;public class Solution {/*** @param root1: the first tree* @param root2: the second tree* @return: returns whether the leaf sequence is the same*/public boolean leafSimilar(TreeNode root1, TreeNode root2) {// write your code here.List<Integer> leaves1 = new ArrayList<>(), leaves2 = new ArrayList<>();dfs(root1, leaves1);dfs(root2, leaves2);if (leaves1.size() != leaves2.size()) {return false;}for (int i = 0; i < leaves1.size(); i++) {if (!leaves1.get(i).equals(leaves2.get(i))) {return false;}}return true;}private void dfs(TreeNode root, List<Integer> leaves) {if (root == null) {return;}// 碰到叶子则加入列表中if (root.left == null && root.right == null) {leaves.add(root.val);return;}// 然后分别收集左右子树中的叶子dfs(root.left, leaves);dfs(root.right, leaves);}
}class TreeNode {int val;TreeNode left, right;TreeNode(int x) {val = x;}
}

时空复杂度 O ( n 1 + n 2 ) O(n_1+n_2) O(n1​+n2​)。

【Lintcode】1495. Leaf-Similar Trees相关推荐

  1. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  2. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  3. 【Lintcode】018.Subsets II

    题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...

  4. 【Lintcode】1413. Tree

    题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...

  5. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  6. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  7. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

  8. 【模拟】相似度 (similar.pas/c/cpp)

    相似度 similar.pas/c/cpp 问题描述: 有两个长度相等的DNA序列(只包含A,T,G,C的字符串),我们想知道它的相似度,它的相似度是这样定义的. 首先相似度为0. 若在对应位置出现A ...

  9. 【Lintcode】1799. Sequence Summation

    题目地址: https://www.lintcode.com/problem/sequence-summation/description 给定三个数i,j,ki,j,ki,j,k,计算i+(i+1) ...

最新文章

  1. 微信小程序城市定位(百度地图API)
  2. GaussianView5对分子结构.gjf文件的解读
  3. LeetCode-剑指 Offer 27. 二叉树的镜像
  4. wxWidgets:使用 wxWidgets 的示例控制台(与 GUI 相对)程序
  5. 用自己的数据集在R-FCN框架下进行检测
  6. Hadoop的多节点集群启动,唯独没有namenode进程?(血淋淋教训,一定拍快照)(四十五)...
  7. GCD HDU - 1695
  8. 3dmax体积雾渲染不出来_corona渲染,非常实用的小技巧
  9. 价值1000毕业设计校园信息发布平台网站源码
  10. 爬虫演练-动态的抓取cp网站数据的演练-注意要反爬
  11. monkey测试小结
  12. 计算机组成原理—基本概念(不基础的部分)
  13. Visual C#.Net 网络程序开发-Socket篇
  14. 中国卫生健康统计年鉴(2006-2021年)
  15. c++之QT软件封装(windows)
  16. 西部素质教育杂志西部素质教育杂志社西部素质教育编辑部2022年第13期目录
  17. 《拆掉思维里的墙》读后感
  18. JAVA1.8开发工具使用系列 故障排除之jcmd
  19. 编程之美 1.8 小飞的电梯调度算法
  20. 【番杰的问答笔记】摄氏度与华氏度的转换

热门文章

  1. EaseUS RecExperts for mac(萤幕录影软件)
  2. 入门级: WinForm 下的 ComboBox,ListBox 的使用 (三) 选择控件
  3. HTML网页设计新闻中心介绍,介绍网站网页设计webdesignernews
  4. 手把手教大家word文档的动态生成
  5. 微信小程序悬浮可拖动客服组件
  6. 文本文件和二进制文件的定义与区别
  7. 什么绑架了我们的注意力
  8. 梦幻仙缘剧情java_梦幻仙缘红包版
  9. 店盈通:何为小渗透品牌?
  10. stream和迭代器的区别