Skip to content

Commit

Permalink
Merge pull request #110 from Joshua-Lu/patch-27
Browse files Browse the repository at this point in the history
更新 0101.对称二叉树 Java版本,递归与迭代
  • Loading branch information
youngyangyang04 authored May 14, 2021
2 parents af95c8c + cbf41eb commit 9fc2f69
Showing 1 changed file with 77 additions and 62 deletions.
139 changes: 77 additions & 62 deletions problems/0101.对称二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,93 +254,108 @@ public:
## 其他语言版本

Java:
```java
public class N0101 {

```Java
/**
* 解法1:DFS,递归。
* 递归法
*/
public boolean isSymmetric2(TreeNode root) {
if (root == null) {
return false;
}

public boolean isSymmetric1(TreeNode root) {
return compare(root.left, root.right);
}

private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;

if (left == null && right != null) {
return false;
}
if (left != null && right == null) {
return false;
}
if (left == null && right != null) {

if (left == null && right == null) {
return true;
}
if (left.val != right.val) {
return false;
}
// 比较外侧
boolean compareOutside = compare(left.left, right.right);
// 比较内侧
boolean compareInside = compare(left.right, right.left);
return compareOutside && compareInside;
}

if (left.val == right.val) {
return compare(left.left, right.right) && compare(left.right, right.left);
/**
* 迭代法
* 使用双端队列,相当于两个栈
*/
public boolean isSymmetric2(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerLast(root.right);
while (!deque.isEmpty()) {
TreeNode leftNode = deque.pollFirst();
TreeNode rightNode = deque.pollLast();
if (leftNode == null && rightNode == null) {
continue;
}
// if (leftNode == null && rightNode != null) {
// return false;
// }
// if (leftNode != null && rightNode == null) {
// return false;
// }
// if (leftNode.val != rightNode.val) {
// return false;
// }
// 以上三个判断条件合并
if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
return false;
}
deque.offerFirst(leftNode.left);
deque.offerFirst(leftNode.right);
deque.offerLast(rightNode.right);
deque.offerLast(rightNode.left);
}

return false;
return true;
}

/**
* 解法2:DFS,迭代
* 迭代法
* 使用普通队列
*/
public boolean isSymmetric3(TreeNode root) {
if (root == null) {
return false;
}

if (!equal(root.left, root.right)) {
return false;
}

Deque<TreeNode> st = new LinkedList<>();

st.push(root.right);
st.push(root.left);

TreeNode curR = root.right;
TreeNode curL = root.left;

while (!st.isEmpty()) {
curL = st.pop();
curR = st.pop();

// 前序,处理
if (!equal(curL, curR)) {
return false;
Queue<TreeNode> deque = new LinkedList<>();
deque.offer(root.left);
deque.offer(root.right);
while (!deque.isEmpty()) {
TreeNode leftNode = deque.poll();
TreeNode rightNode = deque.poll();
if (leftNode == null && rightNode == null) {
continue;
}

if (curR != null && curL != null) {
st.push(curL.right);
st.push(curR.left);
st.push(curR.right);
st.push(curL.left);
// if (leftNode == null && rightNode != null) {
// return false;
// }
// if (leftNode != null && rightNode == null) {
// return false;
// }
// if (leftNode.val != rightNode.val) {
// return false;
// }
// 以上三个判断条件合并
if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
return false;
}
// 这里顺序与使用Deque不同
deque.offer(leftNode.left);
deque.offer(rightNode.right);
deque.offer(leftNode.right);
deque.offer(rightNode.left);
}

return true;
}

private boolean equal(TreeNode l, TreeNode r) {
if (l == null && r == null) {
return true;
}
if (l != null && r == null) {
return false;
}
if (l == null && r != null) {
return false;
}
if (l.val == r.val) {
return true;
}
return false;
}
}
```

Python:
Expand Down

0 comments on commit 9fc2f69

Please sign in to comment.