标签:null class nbsp boolean code *** 递归 条件 smi
思路:
递归方法是最容易想到的
*****.结束条件判定需要注意:1) 当前节点为空则返回true 2)当前节点左右子树有一个为空 返回false 3)当前节点左右子树都不为空,但值不相等返回false
4)左右子树不为空,且节点相等,需要进行下一步判定·····重复1~4
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 private boolean ismirror(TreeNode left, TreeNode right){ 12 if(left==null&&right==null){ 13 return true; 14 }else if(left==null||right==null){ 15 return false; 16 }else if(left.val!=right.val){ 17 return false; 18 }else{ 19 return ismirror(left.left,right.right)&&ismirror(left.right,right.left); 20 } 21 } 22 public boolean isSymmetric(TreeNode root) { 23 if(root == null)return true; 24 return ismirror(root.left,root.right); 25 } 26 }
标签:null class nbsp boolean code *** 递归 条件 smi
原文地址:https://www.cnblogs.com/ScarecrowAnBird/p/12996462.html