1. 程式人生 > >leetcode Symmetric Tree 判斷是不是對稱樹

leetcode Symmetric Tree 判斷是不是對稱樹

使用迭代方法:

public boolean isSymmetric(TreeNode root) {
		if(root==null){
			return true;
		}
		if(root!=null){
			return leftAndRightTreeNode(root.left,root.right);
		}
		return true;
	}
	public boolean leftAndRightTreeNode(TreeNode left,TreeNode right){
		if(left==null && right==null){
			return true;
		}
		if(left==null && right!=null){
			return false;
		}
		if(left!=null && right==null){
			return false;
		}
		if(left!=null && right!=null){
			if(left.val==right.val){
				return leftAndRightTreeNode(left.left,right.right)&& leftAndRightTreeNode(left.right,right.left);
			}
		}
		return false;
	}