Skip to content
Merged
32 changes: 31 additions & 1 deletion problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,36 @@ func levelOrder(root *TreeNode) [][]int {

return res
}

/**
102. 二叉树的层序遍历:使用切片模拟队列,易理解
*/
func levelOrder(root *TreeNode) (res [][]int) {
if root == nil {
return
}

curLevel := []*TreeNode{root} // 存放当前层节点
for len(curLevel) > 0 {
nextLevel := []*TreeNode{} // 准备通过当前层生成下一层
vals := []int{}

for _, node := range curLevel {
vals = append(vals, node.Val) // 收集当前层的值
// 收集下一层的节点
if node.Left != nil {
nextLevel = append(nextLevel, node.Left)
}
if node.Right != nil {
nextLevel = append(nextLevel, node.Right)
}
}
res = append(res, vals)
curLevel = nextLevel // 将下一层变成当前层
}

return
}
```

javascript代码:
Expand Down Expand Up @@ -1072,7 +1102,6 @@ public class N0637 {

que.offerLast(root);
while (!que.isEmpty()) {
TreeNode peek = que.peekFirst();

int levelSize = que.size();
double levelSum = 0.0;
Expand Down Expand Up @@ -2986,3 +3015,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

20 changes: 20 additions & 0 deletions problems/0108.将有序数组转换为二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,26 @@ object Solution {
}
```

## rust

递归:

```rust
impl Solution {
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
if nums.is_empty() {
return None;
}
let index = nums.len() / 2;
let mut root = TreeNode::new(nums[index]);

root.left = Self::sorted_array_to_bst(nums[..index].to_vec());
root.right = Self::sorted_array_to_bst(nums[index + 1..].to_vec());
Some(Rc::new(RefCell::new(root)))
}
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
49 changes: 49 additions & 0 deletions problems/0127.单词接龙.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,55 @@ var ladderLength = function(beginWord, endWord, wordList) {
};
```

## TypeScript
```typescript
function ladderLength(
beginWord: string,
endWord: string,
wordList: string[]
): number {
const words = new Set(wordList);
if (!words.has(endWord)) return 0;
if (beginWord.length === 1) return 2;
let current = new Set([beginWord]);
let rightcurrent = new Set([endWord]);
words.delete(endWord);
let step = 1;
while (current.size) {
if (current.size > rightcurrent.size) {
[current, rightcurrent] = [rightcurrent, current];
}
const temp: Set<string> = new Set();
for (const word of current) {
for (const right of rightcurrent) {
if (diffonechar(word, right)) {
return step + 1;
}
}
for (const other of words) {
if (diffonechar(other, word)) {
temp.add(other);

words.delete(other);
}
}
}
if (temp.size === 0) return 0;
current = temp;
step = step + 1;
}
return 0;
}

function diffonechar(word1: string, word2: string): boolean {
let changes = 0;
for (let i = 0; i < word1.length; i++) {
if (word1[i] != word2[i]) changes += 1;
}
return changes === 1;
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
27 changes: 27 additions & 0 deletions problems/0669.修剪二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ object Solution {
}
```

## rust

// 递归
```rust
impl Solution {
pub fn trim_bst(
root: Option<Rc<RefCell<TreeNode>>>,
low: i32,
high: i32,
) -> Option<Rc<RefCell<TreeNode>>> {
root.as_ref()?;
let mut node = root.as_ref().unwrap().borrow_mut();
if node.val < low {
return Self::trim_bst(node.right.clone(), low, high);
}
if node.val > high {
return Self::trim_bst(node.left.clone(), low, high);
}

node.left = Self::trim_bst(node.left.clone(), low, high);
node.right = Self::trim_bst(node.right.clone(), low, high);
drop(node);
root
}
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down