add #7 python implement
This commit is contained in:
parent
8b2fe318ee
commit
23f7a48929
123
notes/面试总结.md
123
notes/面试总结.md
@ -5,6 +5,8 @@
|
||||
* [4. 二维数组中的查找](#4-二维数组中的查找)
|
||||
* [5. 替换空格](#5-替换空格)
|
||||
* [6. 从尾到头打印链表](#6-从尾到头打印链表)
|
||||
* [7. 重建二叉树](#7-重建二叉树)
|
||||
* [8. 二叉树的下一个结点](#8-二叉树的下一个结点)
|
||||
|
||||
* [参考文献](#参考文献)
|
||||
<!-- GFM-TOC -->
|
||||
@ -401,6 +403,19 @@ public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
### 使用 Collections.reverse()
|
||||
|
||||
```java
|
||||
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
|
||||
ArrayList<Integer> ret = new ArrayList<>();
|
||||
while (listNode != null) {
|
||||
ret.add(listNode.val);
|
||||
listNode = listNode.next;
|
||||
}
|
||||
Collections.reverse(ret);
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
def printListFromTailToHead(listNode):
|
||||
@ -412,6 +427,114 @@ def printListFromTailToHead(listNode):
|
||||
l.reverse()
|
||||
return l
|
||||
```
|
||||
# 7. 重建二叉树
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
根据二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
|
||||
|
||||
```html
|
||||
preorder = [3,9,20,15,7]
|
||||
inorder = [9,3,15,20,7]
|
||||
```
|
||||
|
||||
<div align="center"> <img src="../pics//8a4c6ad4-a816-47d1-b93f-7ca4f78ab67a.png" width="250"/> </div><br>
|
||||
|
||||
## 解题思路
|
||||
|
||||
前序遍历的第一个值为根节点的值,使用这个值将中序遍历结果分成两部分,左部分为树的左子树中序遍历结果,右部分为树的右子树中序遍历的结果。
|
||||
|
||||
```java
|
||||
// 缓存中序遍历数组每个值对应的索引
|
||||
private Map<Integer, Integer> indexForInOrders = new HashMap<>();
|
||||
|
||||
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
|
||||
for (int i = 0; i < in.length; i++)
|
||||
indexForInOrders.put(in[i], i);
|
||||
return reConstructBinaryTree(pre, 0, pre.length - 1, 0);
|
||||
}
|
||||
|
||||
private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int inL) {
|
||||
if (preL > preR)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(pre[preL]);
|
||||
int inIndex = indexForInOrders.get(root.val);
|
||||
int leftTreeSize = inIndex - inL;
|
||||
root.left = reConstructBinaryTree(pre, preL + 1, preL + leftTreeSize, inL);
|
||||
root.right = reConstructBinaryTree(pre, preL + leftTreeSize + 1, preR, inL + leftTreeSize + 1);
|
||||
return root;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# 返回构造的TreeNode根节点
|
||||
def reConstructBinaryTree(self, pre, tin):
|
||||
# write code here
|
||||
if not pre or not tin:
|
||||
return None
|
||||
root = TreeNode(pre.pop(0))
|
||||
index = tin.index(root.val)
|
||||
root.left = self.reConstructBinaryTree(pre, tin[:index])
|
||||
root.right = self.reConstructBinaryTree(pre, tin[index + 1:])
|
||||
return root
|
||||
```
|
||||
|
||||
# 8. 二叉树的下一个结点
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e?tpId=13&tqId=11210&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
|
||||
|
||||
```java
|
||||
public class TreeLinkNode {
|
||||
int val;
|
||||
TreeLinkNode left = null;
|
||||
TreeLinkNode right = null;
|
||||
TreeLinkNode next = null;
|
||||
|
||||
TreeLinkNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 解题思路
|
||||
|
||||
① 如果一个节点的右子树不为空,那么该节点的下一个节点是右子树的最左节点;
|
||||
|
||||
<div align="center"> <img src="../pics//cb0ed469-27ab-471b-a830-648b279103c8.png" width="250"/> </div><br>
|
||||
|
||||
② 否则,向上找第一个左链接指向的树包含该节点的祖先节点。
|
||||
|
||||
<div align="center"> <img src="../pics//e143f6da-d114-4ba4-8712-f65299047fa2.png" width="250"/> </div><br>
|
||||
|
||||
```java
|
||||
public TreeLinkNode GetNext(TreeLinkNode pNode) {
|
||||
if (pNode.right != null) {
|
||||
TreeLinkNode node = pNode.right;
|
||||
while (node.left != null)
|
||||
node = node.left;
|
||||
return node;
|
||||
} else {
|
||||
while (pNode.next != null) {
|
||||
TreeLinkNode parent = pNode.next;
|
||||
if (parent.left == pNode)
|
||||
return parent;
|
||||
pNode = pNode.next;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
|
||||
```
|
||||
|
||||
|
||||
# 参考文献
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user