mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0101.对称二叉树.md Java版本
This commit is contained in:
@ -254,7 +254,94 @@ public:
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
public class N0101 {
|
||||||
|
/**
|
||||||
|
* 解法1:DFS,递归。
|
||||||
|
*/
|
||||||
|
public boolean isSymmetric2(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.val == right.val) {
|
||||||
|
return compare(left.left, right.right) && compare(left.right, right.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解法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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curR != null && curL != null) {
|
||||||
|
st.push(curL.right);
|
||||||
|
st.push(curR.left);
|
||||||
|
st.push(curR.right);
|
||||||
|
st.push(curL.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:
|
Python:
|
||||||
|
|
||||||
@ -283,4 +370,4 @@ const check = (leftPtr, rightPtr) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user