100. Same Tree

    xiaoxiao2021-03-25  63

    Given two binary trees, write a function to check if they are equal or not.

    Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

    问题:给两棵二叉树判断它们是否相等,二叉树相等指两棵树在结构上一样,并且同一位置的结点所带的值相等。

    思想:递归

    boolean flag=true; public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null&&q!=null||p!=null&&q==null) return flag=false; if(p!=null&&q!=null){ if(p.val!=q.val) { System.out.println("p.val="+p.val+"\tq.val="+q.val); return flag=false; }else{ //System.out.println("p.val="+p.val+"\tq.val="+q.val); isSameTree(p.left,q.left); isSameTree(p.right,q.right); } } return flag; }

    转载请注明原文地址: https://ju.6miu.com/read-34608.html

    最新回复(0)