parent
9cd6f13e54
commit
d31cff59f5
104
notes/面试总结.md
104
notes/面试总结.md
@ -28,6 +28,8 @@
|
|||||||
* [23. 链表中环的入口结点](#23-链表中环的入口结点)
|
* [23. 链表中环的入口结点](#23-链表中环的入口结点)
|
||||||
* [24. 反转链表](#24-反转链表)
|
* [24. 反转链表](#24-反转链表)
|
||||||
* [25. 合并两个排序的链表](#25-合并两个排序的链表)
|
* [25. 合并两个排序的链表](#25-合并两个排序的链表)
|
||||||
|
* [26. 树的子结构](#26-树的子结构)
|
||||||
|
* [27. 二叉树的镜像](#27-二叉树的镜像)
|
||||||
|
|
||||||
* [参考文献](#参考文献)
|
* [参考文献](#参考文献)
|
||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
@ -1965,6 +1967,108 @@ class Solution:
|
|||||||
cur.next = pHead2
|
cur.next = pHead2
|
||||||
return head.next
|
return head.next
|
||||||
```
|
```
|
||||||
|
# 26. 树的子结构
|
||||||
|
|
||||||
|
[NowCoder](https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88?tpId=13&tqId=11170&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
<div align="center"> <img src="../pics//4583e24f-424b-4d50-8a14-2c38a1827d4a.png" width="500"/> </div><br>
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
```java
|
||||||
|
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
|
||||||
|
if (root1 == null || root2 == null)
|
||||||
|
return false;
|
||||||
|
return isSubtreeWithRoot(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSubtreeWithRoot(TreeNode root1, TreeNode root2) {
|
||||||
|
if (root2 == null)
|
||||||
|
return true;
|
||||||
|
if (root1 == null)
|
||||||
|
return false;
|
||||||
|
if (root1.val != root2.val)
|
||||||
|
return false;
|
||||||
|
return isSubtreeWithRoot(root1.left, root2.left) && isSubtreeWithRoot(root1.right, root2.right);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
# class TreeNode:
|
||||||
|
# def __init__(self, x):
|
||||||
|
# self.val = x
|
||||||
|
# self.left = None
|
||||||
|
# self.right = None
|
||||||
|
class Solution:
|
||||||
|
def HasSubtree(self, pRoot1, pRoot2):
|
||||||
|
# write code here
|
||||||
|
if pRoot1 == None or pRoot2 == None:
|
||||||
|
return False
|
||||||
|
return self.isSubtreeWithRoot(pRoot1, pRoot2) or \
|
||||||
|
self.HasSubtree(pRoot1.left, pRoot2) or \
|
||||||
|
self.HasSubtree(pRoot1.right, pRoot2)
|
||||||
|
|
||||||
|
def isSubtreeWithRoot(self, pRoot1, pRoot2):
|
||||||
|
if pRoot2 == None:
|
||||||
|
return True
|
||||||
|
if pRoot1 == None:
|
||||||
|
return False
|
||||||
|
if pRoot1.val != pRoot2.val:
|
||||||
|
return False
|
||||||
|
return self.isSubtreeWithRoot(pRoot1.left, pRoot2.left) and \
|
||||||
|
self.isSubtreeWithRoot(pRoot1.right, pRoot2.right)
|
||||||
|
```
|
||||||
|
|
||||||
|
# 27. 二叉树的镜像
|
||||||
|
|
||||||
|
[NowCoder](https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||||
|
|
||||||
|
## 题目描述
|
||||||
|
|
||||||
|
<div align="center"> <img src="../pics//a2d13178-f1ef-4811-a240-1fe95b55b1eb.png" width="300"/> </div><br>
|
||||||
|
|
||||||
|
## 解题思路
|
||||||
|
|
||||||
|
```java
|
||||||
|
public void Mirror(TreeNode root) {
|
||||||
|
if (root == null)
|
||||||
|
return;
|
||||||
|
swap(root);
|
||||||
|
Mirror(root.left);
|
||||||
|
Mirror(root.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void swap(TreeNode root) {
|
||||||
|
TreeNode t = root.left;
|
||||||
|
root.left = root.right;
|
||||||
|
root.right = t;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
# class TreeNode:
|
||||||
|
# def __init__(self, x):
|
||||||
|
# self.val = x
|
||||||
|
# self.left = None
|
||||||
|
# self.right = None
|
||||||
|
class Solution:
|
||||||
|
# 返回镜像树的根节点
|
||||||
|
def Mirror(self, root):
|
||||||
|
# write code here
|
||||||
|
if root == None:
|
||||||
|
return
|
||||||
|
self.swap(root)
|
||||||
|
self.Mirror(root.left)
|
||||||
|
self.Mirror(root.right)
|
||||||
|
|
||||||
|
|
||||||
|
def swap(self, root):
|
||||||
|
root.left, root.right = root.right, root.left
|
||||||
|
```
|
||||||
|
|
||||||
# 参考文献
|
# 参考文献
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user