pull/944/head
krahets 1 year ago
parent 33edc752c8
commit 221bec3ea3

@ -77,8 +77,7 @@ status: new
```python title="build_tree.py" ```python title="build_tree.py"
def dfs( def dfs(
preorder: list[int], preorder: list[int],
inorder: list[int], inorder_map: dict[int, int],
hmap: dict[int, int],
i: int, i: int,
l: int, l: int,
r: int, r: int,
@ -90,19 +89,19 @@ status: new
# 初始化根节点 # 初始化根节点
root = TreeNode(preorder[i]) root = TreeNode(preorder[i])
# 查询 m ,从而划分左右子树 # 查询 m ,从而划分左右子树
m = hmap[preorder[i]] m = inorder_map[preorder[i]]
# 子问题:构建左子树 # 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1) root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)
# 子问题:构建右子树 # 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r) root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)
# 返回根节点 # 返回根节点
return root return root
def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None: def build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:
"""构建二叉树""" """构建二叉树"""
# 初始化哈希表,存储 inorder 元素到索引的映射 # 初始化哈希表,存储 inorder 元素到索引的映射
hmap = {val: i for i, val in enumerate(inorder)} inorder_map = {val: i for i, val in enumerate(inorder)}
root = dfs(preorder, inorder, hmap, 0, 0, len(inorder) - 1) root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)
return root return root
``` ```
@ -110,18 +109,18 @@ status: new
```cpp title="build_tree.cpp" ```cpp title="build_tree.cpp"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode *dfs(vector<int> &preorder, vector<int> &inorder, unordered_map<int, int> &hmap, int i, int l, int r) { TreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return NULL; return NULL;
// 初始化根节点 // 初始化根节点
TreeNode *root = new TreeNode(preorder[i]); TreeNode *root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap[preorder[i]]; int m = inorderMap[preorder[i]];
// 子问题:构建左子树 // 子问题:构建左子树
root->left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root->right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -129,11 +128,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
unordered_map<int, int> hmap; unordered_map<int, int> inorderMap;
for (int i = 0; i < inorder.size(); i++) { for (int i = 0; i < inorder.size(); i++) {
hmap[inorder[i]] = i; inorderMap[inorder[i]] = i;
} }
TreeNode *root = dfs(preorder, inorder, hmap, 0, 0, inorder.size() - 1); TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);
return root; return root;
} }
``` ```
@ -142,18 +141,18 @@ status: new
```java title="build_tree.java" ```java title="build_tree.java"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode dfs(int[] preorder, int[] inorder, Map<Integer, Integer> hmap, int i, int l, int r) { TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return null; return null;
// 初始化根节点 // 初始化根节点
TreeNode root = new TreeNode(preorder[i]); TreeNode root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap.get(preorder[i]); int m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -161,11 +160,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode buildTree(int[] preorder, int[] inorder) { TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Map<Integer, Integer> hmap = new HashMap<>(); Map<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { for (int i = 0; i < inorder.length; i++) {
hmap.put(inorder[i], i); inorderMap.put(inorder[i], i);
} }
TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }
``` ```
@ -174,18 +173,18 @@ status: new
```csharp title="build_tree.cs" ```csharp title="build_tree.cs"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode dfs(int[] preorder, int[] inorder, Dictionary<int, int> hmap, int i, int l, int r) { TreeNode dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) if (r - l < 0)
return null; return null;
// 初始化根节点 // 初始化根节点
TreeNode root = new TreeNode(preorder[i]); TreeNode root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap[preorder[i]]; int m = inorderMap[preorder[i]];
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -193,11 +192,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode buildTree(int[] preorder, int[] inorder) { TreeNode buildTree(int[] preorder, int[] inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Dictionary<int, int> hmap = new Dictionary<int, int>(); Dictionary<int, int> inorderMap = new Dictionary<int, int>();
for (int i = 0; i < inorder.Length; i++) { for (int i = 0; i < inorder.Length; i++) {
hmap.TryAdd(inorder[i], i); inorderMap.TryAdd(inorder[i], i);
} }
TreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.Length - 1); TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.Length - 1);
return root; return root;
} }
``` ```
@ -206,7 +205,7 @@ status: new
```go title="build_tree.go" ```go title="build_tree.go"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode { func dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {
// 子树区间为空时终止 // 子树区间为空时终止
if r-l < 0 { if r-l < 0 {
return nil return nil
@ -214,11 +213,11 @@ status: new
// 初始化根节点 // 初始化根节点
root := NewTreeNode(preorder[i]) root := NewTreeNode(preorder[i])
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
m := hmap[preorder[i]] m := inorderMap[preorder[i]]
// 子问题:构建左子树 // 子问题:构建左子树
root.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1) root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)
// 子问题:构建右子树 // 子问题:构建右子树
root.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r) root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)
// 返回根节点 // 返回根节点
return root return root
} }
@ -226,12 +225,12 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
func buildTree(preorder, inorder []int) *TreeNode { func buildTree(preorder, inorder []int) *TreeNode {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
hmap := make(map[int]int, len(inorder)) inorderMap := make(map[int]int, len(inorder))
for i := 0; i < len(inorder); i++ { for i := 0; i < len(inorder); i++ {
hmap[inorder[i]] = i inorderMap[inorder[i]] = i
} }
root := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1) root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)
return root return root
} }
``` ```
@ -240,7 +239,7 @@ status: new
```swift title="build_tree.swift" ```swift title="build_tree.swift"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
func dfs(preorder: [Int], inorder: [Int], hmap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? { func dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {
// 子树区间为空时终止 // 子树区间为空时终止
if r - l < 0 { if r - l < 0 {
return nil return nil
@ -248,11 +247,11 @@ status: new
// 初始化根节点 // 初始化根节点
let root = TreeNode(x: preorder[i]) let root = TreeNode(x: preorder[i])
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
let m = hmap[preorder[i]]! let m = inorderMap[preorder[i]]!
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1, l: l, r: m - 1) root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: i + 1 + m - l, l: m + 1, r: r) root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)
// 返回根节点 // 返回根节点
return root return root
} }
@ -260,8 +259,8 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? { func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset } let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
return dfs(preorder: preorder, inorder: inorder, hmap: hmap, i: 0, l: 0, r: inorder.count - 1) return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)
} }
``` ```
@ -269,17 +268,17 @@ status: new
```javascript title="build_tree.js" ```javascript title="build_tree.js"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
function dfs(preorder, inorder, hmap, i, l, r) { function dfs(preorder, inorderMap, i, l, r) {
// 子树区间为空时终止 // 子树区间为空时终止
if (r - l < 0) return null; if (r - l < 0) return null;
// 初始化根节点 // 初始化根节点
const root = new TreeNode(preorder[i]); const root = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
const m = hmap.get(preorder[i]); const m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -287,11 +286,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
function buildTree(preorder, inorder) { function buildTree(preorder, inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = new Map(); let inorderMap = new Map();
for (let i = 0; i < inorder.length; i++) { for (let i = 0; i < inorder.length; i++) {
hmap.set(inorder[i], i); inorderMap.set(inorder[i], i);
} }
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }
``` ```
@ -302,8 +301,7 @@ status: new
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
function dfs( function dfs(
preorder: number[], preorder: number[],
inorder: number[], inorderMap: Map<number, number>,
hmap: Map<number, number>,
i: number, i: number,
l: number, l: number,
r: number r: number
@ -313,11 +311,11 @@ status: new
// 初始化根节点 // 初始化根节点
const root: TreeNode = new TreeNode(preorder[i]); const root: TreeNode = new TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
const m = hmap.get(preorder[i]); const m = inorderMap.get(preorder[i]);
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -325,11 +323,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
function buildTree(preorder: number[], inorder: number[]): TreeNode | null { function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let hmap = new Map<number, number>(); let inorderMap = new Map<number, number>();
for (let i = 0; i < inorder.length; i++) { for (let i = 0; i < inorder.length; i++) {
hmap.set(inorder[i], i); inorderMap.set(inorder[i], i);
} }
const root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }
``` ```
@ -340,8 +338,7 @@ status: new
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
TreeNode? dfs( TreeNode? dfs(
List<int> preorder, List<int> preorder,
List<int> inorder, Map<int, int> inorderMap,
Map<int, int> hmap,
int i, int i,
int l, int l,
int r, int r,
@ -353,11 +350,11 @@ status: new
// 初始化根节点 // 初始化根节点
TreeNode? root = TreeNode(preorder[i]); TreeNode? root = TreeNode(preorder[i]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
int m = hmap[preorder[i]]!; int m = inorderMap[preorder[i]]!;
// 子问题:构建左子树 // 子问题:构建左子树
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
return root; return root;
} }
@ -365,11 +362,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
TreeNode? buildTree(List<int> preorder, List<int> inorder) { TreeNode? buildTree(List<int> preorder, List<int> inorder) {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
Map<int, int> hmap = {}; Map<int, int> inorderMap = {};
for (int i = 0; i < inorder.length; i++) { for (int i = 0; i < inorder.length; i++) {
hmap[inorder[i]] = i; inorderMap[inorder[i]] = i;
} }
TreeNode? root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1); TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);
return root; return root;
} }
``` ```
@ -378,17 +375,17 @@ status: new
```rust title="build_tree.rs" ```rust title="build_tree.rs"
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> { fn dfs(preorder: &[i32], inorderMap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {
// 子树区间为空时终止 // 子树区间为空时终止
if r - l < 0 { return None; } if r - l < 0 { return None; }
// 初始化根节点 // 初始化根节点
let root = TreeNode::new(preorder[i as usize]); let root = TreeNode::new(preorder[i as usize]);
// 查询 m ,从而划分左右子树 // 查询 m ,从而划分左右子树
let m = hmap.get(&preorder[i as usize]).unwrap(); let m = inorderMap.get(&preorder[i as usize]).unwrap();
// 子问题:构建左子树 // 子问题:构建左子树
root.borrow_mut().left = dfs(preorder, inorder, hmap, i + 1, l, m - 1); root.borrow_mut().left = dfs(preorder, inorderMap, i + 1, l, m - 1);
// 子问题:构建右子树 // 子问题:构建右子树
root.borrow_mut().right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r); root.borrow_mut().right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
// 返回根节点 // 返回根节点
Some(root) Some(root)
} }
@ -396,11 +393,11 @@ status: new
/* 构建二叉树 */ /* 构建二叉树 */
fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> { fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let mut hmap: HashMap<i32, i32> = HashMap::new(); let mut inorderMap: HashMap<i32, i32> = HashMap::new();
for i in 0..inorder.len() { for i in 0..inorder.len() {
hmap.insert(inorder[i], i as i32); inorderMap.insert(inorder[i], i as i32);
} }
let root = dfs(preorder, inorder, &hmap, 0, 0, inorder.len() as i32 - 1); let root = dfs(preorder, &inorderMap, 0, 0, inorder.len() as i32 - 1);
root root
} }
``` ```

@ -249,7 +249,26 @@ comments: true
=== "Rust" === "Rust"
```rust title="hash_map.rs" ```rust title="hash_map.rs"
use std::collections::HashMap;
/* 初始化哈希表 */
let mut map: HashMap<i32, String> = HashMap::new();
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
map.insert(12836, "小哈".to_string());
map.insert(15937, "小啰".to_string());
map.insert(16750, "小算".to_string());
map.insert(13279, "小法".to_string());
map.insert(10583, "小鸭".to_string());
/* 查询操作 */
// 向哈希表中输入键 key ,得到值 value
let _name: Option<&String> = map.get(&15937);
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
let _removed_value: Option<String> = map.remove(&10583);
``` ```
=== "C" === "C"
@ -430,7 +449,21 @@ comments: true
=== "Rust" === "Rust"
```rust title="hash_map.rs" ```rust title="hash_map.rs"
/* 遍历哈希表 */
// 遍历键值对 Key->Value
for (key, value) in &map {
println!("{key} -> {value}");
}
// 单独遍历键 Key
for key in map.keys() {
println!("{key}");
}
// 单独遍历值 Value
for value in map.values() {
println!("{value}");
}
``` ```
=== "C" === "C"

@ -276,7 +276,32 @@ comments: true
=== "Rust" === "Rust"
```rust title="stack.rs" ```rust title="stack.rs"
/* 初始化栈 */
// 把 Vec 当作栈来使用
let mut stack: Vec<i32> = Vec::new();
/* 元素入栈 */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
/* 访问栈顶元素 */
if let Some(top) = stack.get(stack.len() - 1) {
}
if let Some(top) = stack.last() {
}
/* 元素出栈 */
if let Some(pop) = stack.pop() {
}
/* 获取栈的长度 */
let size = stack.len();
/* 判断是否为空 */
let isEmpty = stack.is_empty();
``` ```
=== "C" === "C"

@ -972,7 +972,7 @@ comments: true
cur = cur.left; cur = cur.left;
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == null || pre == null) if (cur == null)
return; return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {

Loading…
Cancel
Save