diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 07f57455cc..406242a786 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -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代码: @@ -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; @@ -2986,3 +3015,4 @@ impl Solution { + diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 01c276fd83..c75f87d1e0 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -486,6 +486,26 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn sorted_array_to_bst(nums: Vec) -> Option>> { + 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))) + } +} +``` +

diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 2e30123d16..bc21d0ade1 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -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 = 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; +} +``` +

diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 1c5fdea108..ac36509f21 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -473,6 +473,33 @@ object Solution { } ``` +## rust + +// 递归 +```rust +impl Solution { + pub fn trim_bst( + root: Option>>, + low: i32, + high: i32, + ) -> Option>> { + 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 + } +} +``` +