<span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ package javaTrain; public class Train8 { public boolean isSymmetric(TreeNode root) { if(root == null) return true; if(root.left == null && root.right == null) return true; else if(root.left == null || root.right == null) return false; return help(root.left,root.right); } private boolean help(TreeNode left,TreeNode right){ if(left == null && right == null) return true; else if(left == null || right == null) return false; if(left.val == right.val) return help(left.left,right.right ) && help(left.right,right.left); else return false; } } </span>
时间: 2024-10-11 07:49:14