class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root:return True def Tree(p,q): if not p and not q:return True if p and q and p.val==q.val: return Tree(p.left,q.right) and Tree(p.right,q.left) return False return Tree(root.left,root.right)
执行用时 :24 ms, 在所有 python 提交中击败了76.38%的用户
内存消耗 :12 MB, 在所有 python 提交中击败了14.26%的用户
——2019.11.10
还是运用递归的思想,挺简单好用的。
public boolean isSymmetric(TreeNode root) { if(root == null){ return true; } return isSymmetric(root.left,root.right); } private boolean isSymmetric(TreeNode left, TreeNode right) { if(left == null && right == null){ return true; }else if(left == null || right == null){ return false; }else if(left.val != right.val){ return false; } return isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left); }
——2020.7.1