Lc101_对称二叉树
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSame(root.left, root.right);
}
/**
* 优化是对于if判断,本来三个if判断,优化为俩个效率从1ms降到0ms
* @param left
* @param right
* @return
*/
static boolean isSame(TreeNode left, TreeNode right) {
if (left == null && right != null) {
return false;
} else if (left == null|| right == null) {
return true;
}
return left.val == right.val &&isSame(left.left, right.right) && isSame(left.right, right.left);
}
不恋尘世浮华,不写红尘纷扰