标签:cal roo int div ring iss 一个 return als
/** * * @author gentleKay * 题目描述 * 请实现一个函数,用来判断一颗二叉树是不是对称的。 * 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 */ public class Main56 { public static void main(String[] args) { // TODO Auto-generated method stub TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(2); root.left.left = new TreeNode(3); root.left.right = new TreeNode(4); root.right.left = new TreeNode(4); root.right.right = new TreeNode(3); // root.left.left.left = new TreeNode(5); System.out.println(Main56.isSymmetrical(root)); } public static class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } static boolean isSymmetrical(TreeNode pRoot){ if (pRoot == null) { return true; } return isSym(pRoot.left, pRoot.right); } static boolean isSym(TreeNode RootLeft, TreeNode RootRight) { if (RootLeft == null && RootRight == null) { return true; } if (RootLeft == null || RootRight == null) { return false; } if (RootLeft.val == RootRight.val) { return isSym(RootLeft.left,RootRight.right) && isSym(RootLeft.right, RootRight.left); } return false; } }
标签:cal roo int div ring iss 一个 return als
原文地址:https://www.cnblogs.com/strive-19970713/p/11202637.html