对称二叉树
1.介绍
2.解决思想
递归方式实现
3.代码
public static boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetric2(root.left, root.right); } public static boolean isSymmetric2(TreeNode root1, TreeNode root2) { if (root1 == null && root2 == null) return true; if (root1 == null || root2 == null) return false; return (root1.val == root2.val) && isSymmetric2(root1.left, root2.right) && isSymmetric2(root1.right, root2.left); }
------------------------------------------------------------------------------------------------------------------------------------------------- 觉得我的博客有用的,请顶下我的文章并关注我,让更多的人能看到我写的博客,谢谢。-------------------------------------------------------------------------------------------------------------------------------------------------
网上的答案很多,但是怎么说呢,太多都是那种复制粘贴的答案,有些都是运行不了的。我写博客的原因主要是为了分享我的学习过程,帮助大家,同时也让其他人检查我的答案。太过简单的博客我就不写了,浪费彼此时间。
首先我保证,我的代码都是自己运行过的,如果是同样的java1.7是不会有问题。
其次,我的代码都是写清楚了解决思想的,便于大家理解。
我写代码主要是看中可读性,太过复杂的代码,在我看来是很扯淡的。代码的可读性很重要,尤其是在项目中,一个项目的代码如果需要他人阅读半天才能理解,那么这个项目会越来越难处理。