其他分享
首页 > 其他分享> > (22) 判断二叉树是否对称

(22) 判断二叉树是否对称

作者:互联网

一、问题描述

给定一个二叉树判定其是否对称

 

二、Code

 1 package algorithm;
 2 
 3 /**
 4  * Created by adrian.wu on 2019/5/30.
 5  */
 6 
 7 
 8 /*
 9 说明:
10 其实就是类似与前序遍历然后遍历的顺序换了一下,即左边遍历的同时也在遍历最右边,然后比较两个的值
11  */
12 public class BinaryTreeSymmetricalJudger {
13     public static class BinaryTree {
14         BinaryTree left, right;
15         int val;
16 
17         public BinaryTree(int val) {
18             this.val = val;
19         }
20     }
21 
22     public static boolean isSymmetrical(BinaryTree head) {
23         return helper(head.left, head.right);
24     }
25 
26     private static boolean helper(BinaryTree left, BinaryTree right) {
27         if (left == null && right == null) return true;
28         if (left == null || right == null) return false;
29         if (left.val != right.val) return false;
30 
31         return helper(left.left, right.right) && helper(left.right, right.left);
32     }
33 }

 

标签:right,return,val,22,BinaryTree,二叉树,helper,对称,left
来源: https://www.cnblogs.com/ylxn/p/10949185.html