示例记录(二)

1、判断二叉树是否对称(镜像)二叉树;

举个栗子:       

 

 

 二叉树实体:

 1     /**
 2      * 二叉树
 3      */
 4     public class BinaryTree{
 5     int value;
 6     BinaryTree left=null;
 7     BinaryTree right=null;
 8     public BinaryTree(int value){
 9         this.value=value;
10     }

实现代码:

 1 /**
 2      * 判断二叉树的左节点和右节点对比判断
 3      */
 4     boolean isSymmetrical(BinaryTree root1, BinaryTree root2) {
 5         if (root1 == null && root2 == null) {
 6             return true;
 7         }
 8         if (root1 == null || root2 == null) {
 9             return false;
10         }
11         if (root1.value != root2.value) {
12             return false;
13         }
14         //判断A的左边和B的右边是否相等,判断A的右边和B的左边是否相等,都相等就满足
15         return isSymmetrical(root1.left, root2.right) && isSymmetrical(root1.right, root2.left);
16     }
17 
18     /**
19      * 判断二叉树是否对称二叉树
20      * @param root
21      * @return
22      */
23     boolean isSymmetrical(BinaryTree root){
24         if (null==root){
25             return true;
26         }
27         return isSymmetrical(root.left,root.right);
28     }

 

posted @ 2019-12-24 15:48  阿 飞  阅读(148)  评论(0编辑  收藏  举报