首页 > 编程语言 >LeetCode-Java-872. Leaf-Similar Trees

LeetCode-Java-872. Leaf-Similar Trees

时间:2022-12-14 15:07:34浏览次数:62  
标签:right TreeNode int 872 Trees leaf sqn Java left


题目

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.

LeetCode-Java-872. Leaf-Similar Trees_ide

代码

简单的遍历一遍树存储其叶子节点,最后进行比较即可,注意对两个树遍历的时候方法要一致。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
int[] s1 = list(root1);
int[] s2 = list(root2);
int len1 = s1.length;
int len2 = s2.length;

if(len1!=len2){
return false;
}else{
for(int i=0;i<len1;i++){
if(s1[i]!=s2[i]){
return false;
}
}
}
return true;
}
public static int[] list(TreeNode root){
int [] result = new int[100];
int len = 0;
TreeNode cur = root;
LinkedList<TreeNode> sqn = new LinkedList<TreeNode>();
sqn.addFirst(root);
while(sqn.size()!=0){
TreeNode t = sqn.removeFirst();
if(t.right!=null){
sqn.addFirst(t.right);
}
if(t.left==null&&t.right==null){
result[len++] = t.val;
}
if(t.left!=null){
sqn.addFirst(t.left);
}

}
return Arrays.copyOf(result,len);
}
}


标签:right,TreeNode,int,872,Trees,leaf,sqn,Java,left
From: https://blog.51cto.com/u_12938555/5936973

相关文章

  • 剑指Offer-Java-二叉树的镜像
    题目题目描述操作给定的二叉树,将其变换为源二叉树的镜像。输入描述:二叉树的镜像定义:源二叉树8/\610/\/\57911......
  • 16-咸鱼学Java-内部类补充
    上一篇文章,说了实例内部类和静态内部类,这篇文章重点说明,本地内部类和方法内部类本地内部类也叫本地方法内部类,局部内部类。指在一个方法内定义的类,只有在当前方法中才能对局......
  • LeetCode-Java-136. Single Number
    题目Givenanon-emptyarrayofintegers,everyelementappearstwiceexceptforone.Findthatsingleone.Note:Youralgorithmshouldhavealinearruntimecompl......
  • LeetCode-Java-637. Average of Levels in Binary Tree
    题目Givenanon-emptybinarytree,returntheaveragevalueofthenodesoneachlevelintheformofanarray.Example1:Input:3/\920/\15......
  • Java泛型链表实现
    ​​链表简介​​代码实现packagecom.chenxixuexi;/***泛型链表*单链表逆置数据--节点*求单链表倒数第K节点*求两个单链表是否相交相交交点*判断单链表是否有......
  • 15-咸鱼学Java-内部类
    实例内部类实例内部类简而言之就是类里面嵌入着类,就像环套环一样Demo:classOuterClass2//外部类{privateinta=1000;publicOuterClass2(){Sy......
  • LeetCode-Java-559. Maximum Depth of N-ary Tree
    题目Givenan-arytree,finditsmaximumdepth.Themaximumdepthisthenumberofnodesalongthelongestpathfromtherootnodedowntothefarthestleafnode......
  • 14-咸鱼学Java-面向对象基础:类
    类类就相当于自定义类型,有自己的数据域,有自己的方法。属于一种用户自定义类型。类的目的就是模拟现实中存在的物体,如一个Person类,一个人他有自己的名字,年龄,性别等等,他有自己......
  • 17-咸鱼学Java-内部类实际应用-Java链表
    ​​内部类​​,​​未使用内部类的链表​​可以点击名字查看。此篇文章主要介绍运用内部类的Java链表的写法。链表正如其名,就像一个一个珠子被串起来,只有前一个珠子和......
  • LeetCode-Java-876. Middle of the Linked List
    题目Givenanon-empty,singlylinkedlistwithheadnodehead,returnamiddlenodeoflinkedlist.Iftherearetwomiddlenodes,returnthesecondmiddlenode.......