<span style="font-size:18px;"><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></span>