leetcode 872 叶子相似的树

package com.example.lettcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @Class LeafSimilar
 * @Description 872 叶子相似的树
 * 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。
 * 举个例子,如上图所示,给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树。
 * 如果有两棵二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
 * 如果给定的两个根结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
 * <p>
 * 示例 1:
 * 输入:root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
 * 输出:true
 * <p>
 * 示例 2:
 * 输入:root1 = [1], root2 = [1]
 * 输出:true
 * <p>
 * 示例 3:
 * 输入:root1 = [1], root2 = [2]
 * 输出:false
 * <p>
 * 示例 4:
 * 输入:root1 = [1,2], root2 = [2,2]
 * 输出:true
 * <p>
 * 示例 5:
 * 输入:root1 = [1,2,3], root2 = [1,3,2]
 * 输出:false
 * @Author
 * @Date 2021/5/10
 **/
public class LeafSimilar {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> integerList1 = new ArrayList<>();
        List<Integer> integerList2 = new ArrayList<>();
        recur(root1, integerList1);
        recur(root2, integerList2);
        if (integerList1.size() != integerList2.size()) return false;
        int len1 = integerList1.size();
        int len2 = integerList2.size();
        int index = 0;
        while (index < len1 && index < len2) {
            if (integerList1.get(index).intValue() != integerList2.get(index).intValue()) return false;
            index++;
        }
        return index == len1;
    }

    private static void recur(TreeNode root, List<Integer> integerList) {
        if (root == null) return;
        if (root.left == null && root.right == null) integerList.add(root.val);
        if (root.left != null) recur(root.left, integerList);
        if (root.right != null) recur(root.right, integerList);
    }
}
// 测试用例
public static void main(String[] args) {
	// root1 = [3,5,1,6,2,9,8,null,null,7,4]
	TreeNode root1 = new TreeNode(3);
	TreeNode treeNode01 = new TreeNode(5);
	TreeNode treeNode02 = new TreeNode(1);
	root1.left = treeNode01;
	root1.right = treeNode02;
	TreeNode treeNode03 = new TreeNode(6);
	TreeNode treeNode04 = new TreeNode(2);
	treeNode01.left = treeNode03;
	treeNode01.right = treeNode04;
	TreeNode treeNode05 = new TreeNode(9);
	TreeNode treeNode06 = new TreeNode(8);
	treeNode02.left = treeNode05;
	treeNode02.right = treeNode06;
	TreeNode treeNode07 = new TreeNode(7);
	TreeNode treeNode08 = new TreeNode(4);
	treeNode04.left = treeNode07;
	treeNode05.right = treeNode08;

	// [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
	TreeNode root2 = new TreeNode(3);
	TreeNode treeNode11 = new TreeNode(5);
	TreeNode treeNode12 = new TreeNode(1);
	root2.left = treeNode01;
	root2.right = treeNode02;
	TreeNode treeNode13 = new TreeNode(6);
	TreeNode treeNode14 = new TreeNode(7);
	treeNode11.left = treeNode13;
	treeNode11.right = treeNode14;
	TreeNode treeNode15 = new TreeNode(4);
	TreeNode treeNode16 = new TreeNode(2);
	treeNode12.left = treeNode15;
	treeNode12.right = treeNode16;
	TreeNode treeNode17 = new TreeNode(9);
	TreeNode treeNode18 = new TreeNode(8);
	treeNode16.left = treeNode17;
	treeNode16.right = treeNode18;

	boolean ans = LeafSimilar.leafSimilar(root1, root2);
	System.out.println("LeafSimilar demo01 result : " + ans);

	//root1 = [1], root2 = [1]
	root1 = new TreeNode(1);
	root2 = new TreeNode(1);
	ans = LeafSimilar.leafSimilar(root1, root2);
	System.out.println("LeafSimilar demo02 result : " + ans);

	//root1 = [1], root2 = [2]
	root1 = new TreeNode(1);
	root2 = new TreeNode(2);
	ans = LeafSimilar.leafSimilar(root1, root2);
	System.out.println("LeafSimilar demo03 result : " + ans);

	// [1,2], root2 = [2,2]
	root1 = new TreeNode(1);
	treeNode01 = new TreeNode(2);
	root1.left = treeNode01;
	root2 = new TreeNode(2);
	treeNode11 = new TreeNode(2);
	root2.left = treeNode11;
	ans = LeafSimilar.leafSimilar(root1, root2);
	System.out.println("LeafSimilar demo04 result : " + ans);

	//  [1,2,3], root2 = [1,3,2]
	root1 = new TreeNode(1);
	treeNode01 = new TreeNode(2);
	treeNode02 = new TreeNode(3);
	root1.left = treeNode01;
	root1.right = treeNode02;
	root2 = new TreeNode(1);
	treeNode11 = new TreeNode(3);
	treeNode11 = new TreeNode(2);
	root2.left = treeNode11;
	root2.right = treeNode12;
	ans = LeafSimilar.leafSimilar(root1, root2);
	System.out.println("LeafSimilar demo05 result : " + ans);
}
posted @ 2021-05-12 10:49  枫叶艾辰  阅读(66)  评论(0编辑  收藏  举报