101. Symmetric Tree
和same tree是一样的,就是same tree里比较的是两个树的同一位置,现在是看做这个树和这棵树的翻转
1 public boolean isSymmetric(TreeNode root) { 2 return isSameTree(root, root); 3 } 4 5 private boolean isSameTree(TreeNode p, TreeNode q) { 6 if(p == null && q == null) { 7 return true; 8 } 9 if(p == null || q == null) { 10 return false; 11 } 12 if(p.val != q.val) { 13 return false; 14 } 15 return isSameTree(p.left, q.right) && isSameTree(p.right, q.left); 16 }