首页 > 试题广场 >

相同的二叉树

[编程题]相同的二叉树
  • 热度指数:1544 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定两个根结点分别为 root1root2 二叉树,请判断这两棵树是否完全相同。
数据范围:

两棵树上的节点数目都在范围 [0, 100] 内


示例1

输入

{1,2,1},{1,#,2}

输出

false

说明



两个树在结构上不相同,故它们是不相同的。
示例2

输入

{1,2,1},{1,2,1}

输出

true

说明

两个树在结构上相同,并且节点具有相同的值,故认为它们是相同的。

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
直接遍历,对比节点值是否相同,如果不同返回false,然后递归遍历左节点和右节点;
import java.util.*;

/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/

public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root1 TreeNode类
* @param root2 TreeNode类
* @return bool布尔型
*/
public boolean isSameTree (TreeNode root1, TreeNode root2) {
// write code here
if(root1==null&&root2==null){
return true;
}
最初将判空的逻辑放在了root1.val != root2.va后面,造成程序抛出空指针,调整判断位置成功提交
if((root1==null&&root2!=null)||(root1!=null&&root2==null)){
return false;
}
if (root1.val != root2.val) {
return false;
}
return isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right);
}

}
发表于 2023-11-28 10:57:31 回复(0)
import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root1 TreeNode类 
     * @param root2 TreeNode类 
     * @return bool布尔型
     */
    public boolean isSameTree (TreeNode root1, TreeNode root2) {
        // write code here
        	Queue<TreeNode> rootQueue1 = new LinkedList<>();
		Queue<TreeNode> rootQueue2 = new LinkedList<>();
		rootQueue1.offer(root1);
		rootQueue2.offer(root2);
		while (!rootQueue1.isEmpty() && !rootQueue2.isEmpty()) {
			TreeNode poll1 = rootQueue1.poll();
			TreeNode poll2 = rootQueue2.poll();
            if(poll1 == null && poll2 == null) {
			 continue;
            }
			if (poll1 == null || poll2 == null ||  poll1.val != poll2.val) {
				return false;
			}
			rootQueue1.offer(poll1.left);
			rootQueue1.offer(poll1.right);
			rootQueue2.offer(poll2.left);
			rootQueue2.offer(poll2.right);
		}
		return true;
    }
}

发表于 2022-08-18 23:10:32 回复(0)
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root1 TreeNode类 
     * @param root2 TreeNode类 
     * @return bool布尔型
     */
    public boolean isSameTree (TreeNode root1, TreeNode root2) {
        if(root1 != null && root2 == null || root1 == null && root2 != null) return false;
        if(root1 == null && root2 == null) return true;
        if(root1.val != root2.val) return false;
            return isSameTree(root1.left,root2.left) && isSameTree(root1.right,root2.right);
      
    }
}

发表于 2022-07-12 16:49:57 回复(0)
public boolean isSameTree (TreeNode root1, TreeNode root2) {
        // 如果两棵树都为空返回true
        if (root1 == null && root2 == null) {
            return true;
        }
        // 如果其中一棵树为空返回false
        if (root1 == null || root2 == null) {
            return false;
        }
        // 如果两个节点值不相同返回false
        if (root1.val != root2.val) {
            return false;
        }
        // 递归
        return isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right);
    }
发表于 2022-04-27 15:11:02 回复(0)