Question:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
else
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 == null && right != null)
return false;
else {
return (left.val == right.val) && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
}
}
}
My test result:
**
总结:
这次作业也不是很难吧。
所以一个小时做完了两道 easy级别的题目。
真没啥总结的,就是递归吧。写递归的能力被之前上算法课培训出来了,比较简单的都还是能写写的。。。
加油。
**
Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
ArrayList<TreeNode> l = new ArrayList<TreeNode>(size);
while (!q.isEmpty())
l.add(q.poll());
for (int i = 0; i < size / 2; i++) {
TreeNode left = l.get(i);
TreeNode right = l.get(size - i - 1);
if (left.val != right.val)
return false;
if (left.left == null && right.right != null)
return false;
if (left.left != null && right.right == null)
return false;
if (left.right == null && right.left != null)
return false;
if (left.right != null && right.left == null)
return false;
}
for (int i = 0; i < size; i++) {
TreeNode temp = l.get(i);
if (temp.left != null)
q.offer(temp.left);
if (temp.right != null)
q.offer(temp.right);
}
if (q.size() % 2 == 1)
return false;
}
return true;
}
}
用层序遍历实现了非递归解法。但是时间好长。
用一个队列,存放已经被检查过的结点,按从左往右的顺序。
然后每次进循环,把他们输入进一个list,然后从两端往中间,一个结点一个结点得扫描。判断他们的下一层在结构上是否满足对称。还要检查他们自己的value是否相等。
如果都满足条件。按序把它们的下一层输入进队列。
然后就差不多了。
递归做法:
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
else if (root.left == null && root.right == null)
return true;
else if (root.left == null || root.right == null)
return false;
return isSymmetric(root.left, root.right);
}
private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left.val != right.val)
return false;
boolean b1 = false;
boolean b2 = false;
if (left.left == null && right.right == null)
b1 = true;
else if (left.left == null || right.right == null)
return false;
else
b1 = isSymmetric(left.left, right.right);
if (!b1)
return false;
if (left.right == null && right.left == null)
b2 = true;
else if (left.right == null || right.left == null)
return false;
else
b2 = isSymmetric(left.right, right.left);
return b2;
}
}
意思差不多。
昨晚打电话到两点。又早起上课,一直没停。所以晚上的时候特别累。还得跟着去上课。所以人比较暴躁,心态不好。
晚上回去好好休息。
又拿到了一个OA。好好珍惜。
最近开始练习 Tree。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return helper(root.left, root.right);
}
private boolean helper(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;
}
else {
return helper(left.left, right.right) && helper(left.right, right.left);
}
}
}
recursion 不是很难。
看了上面的 iteration解法,感觉有点烦。
然后看了下答案:
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode n1 = q.poll();
TreeNode n2 = q.poll();
if (n1 == null && n2 == null) {
continue;
}
else if (n1 == null || n2 == null) {
return false;
}
else if (n1.val != n2.val) {
return false;
}
else {
q.offer(n1.left);
q.offer(n2.right);
q.offer(n1.right);
q.offer(n2.left);
}
}
return true;
}
}
reference:
https://leetcode.com/articles/symmetric-tree/
这种层序遍历和之前的不太一样。他是每次压入两个,弹出两个。
Interesting
当分析 recursion 复杂度时,
分析空间的时候,记住考虑到 recursive call 需要在栈中占的体积。
比如这道题目,在最恶劣的情况下,如果树是一条线,那么它所占的体积就是 O(n)
Anyway, Good luck, Richardo! -- 08/28/2016