首页 > 代码库 > Symmetric Tree leetcode java
Symmetric Tree leetcode java
题目:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / 2 2 / \ / 3 4 4 3
But the following is not:
1 / 2 2 \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题解:
判断左右子树是否相等。
递归的解法:
1 public boolean isSymmetricTree(TreeNode p,TreeNode q){
2 if(p == null&&q == null)
3 return true;
4 if(p == null||q == null)
5 return false;
6 return (p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left);
7 }
8
9 public boolean isSymmetric(TreeNode root) {
10 if(root==null)
11 return true;
12
13 return isSymmetricTree(root.left,root.right);
14 }
2 if(p == null&&q == null)
3 return true;
4 if(p == null||q == null)
5 return false;
6 return (p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left);
7 }
8
9 public boolean isSymmetric(TreeNode root) {
10 if(root==null)
11 return true;
12
13 return isSymmetricTree(root.left,root.right);
14 }
非递归解法:
1 public boolean isSymmetric(TreeNode root) {
2 if(root == null)
3 return true;
4 if(root.left == null && root.right == null)
5 return true;
6 if(root.left == null || root.right == null)
7 return false;
8 LinkedList<TreeNode> q1 = new LinkedList<TreeNode>();
9 LinkedList<TreeNode> q2 = new LinkedList<TreeNode>();
10 q1.add(root.left);
11 q2.add(root.right);
12 while(!q1.isEmpty() && !q2.isEmpty()){
13 TreeNode n1 = q1.poll();
14 TreeNode n2 = q2.poll();
15
16 if(n1.val != n2.val)
17 return false;
18 if((n1.left == null && n2.right != null) || (n1.left != null && n2.right == null))
19 return false;
20 if((n1.right == null && n2.left != null) || (n1.right != null && n2.left == null))
21 return false;
22
23 if(n1.left != null && n2.right != null){
24 q1.add(n1.left);
25 q2.add(n2.right);
26 }
27
28 if(n1.right != null && n2.left != null){
29 q1.add(n1.right);
30 q2.add(n2.left);
31 }
32 }
33 return true;
34 }
2 if(root == null)
3 return true;
4 if(root.left == null && root.right == null)
5 return true;
6 if(root.left == null || root.right == null)
7 return false;
8 LinkedList<TreeNode> q1 = new LinkedList<TreeNode>();
9 LinkedList<TreeNode> q2 = new LinkedList<TreeNode>();
10 q1.add(root.left);
11 q2.add(root.right);
12 while(!q1.isEmpty() && !q2.isEmpty()){
13 TreeNode n1 = q1.poll();
14 TreeNode n2 = q2.poll();
15
16 if(n1.val != n2.val)
17 return false;
18 if((n1.left == null && n2.right != null) || (n1.left != null && n2.right == null))
19 return false;
20 if((n1.right == null && n2.left != null) || (n1.right != null && n2.left == null))
21 return false;
22
23 if(n1.left != null && n2.right != null){
24 q1.add(n1.left);
25 q2.add(n2.right);
26 }
27
28 if(n1.right != null && n2.left != null){
29 q1.add(n1.right);
30 q2.add(n2.left);
31 }
32 }
33 return true;
34 }
Reference:
http://blog.csdn.net/linhuanmars/article/details/23072829
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。