Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,62 @@ var searchRange = function(nums, target) {
return [-1, -1];
};
```


### TypeScript

```typescript
function searchRange(nums: number[], target: number): number[] {
const leftBoard: number = getLeftBorder(nums, target);
const rightBoard: number = getRightBorder(nums, target);
// target 在nums区间左侧或右侧
if (leftBoard === (nums.length - 1) || rightBoard === 0) return [-1, -1];
// target 不存在与nums范围内
if (rightBoard - leftBoard <= 1) return [-1, -1];
// target 存在于nums范围内
return [leftBoard + 1, rightBoard - 1];
};
// 查找第一个大于target的元素下标
function getRightBorder(nums: number[], target: number): number {
let left: number = 0,
right: number = nums.length - 1;
// 0表示target在nums区间的左边
let rightBoard: number = 0;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] <= target) {
// 右边界一定在mid右边(不含mid)
left = mid + 1;
rightBoard = left;
} else {
// 右边界在mid左边(含mid)
right = mid - 1;
}
}
return rightBoard;
}
// 查找第一个小于target的元素下标
function getLeftBorder(nums: number[], target: number): number {
let left: number = 0,
right: number = nums.length - 1;
// length-1表示target在nums区间的右边
let leftBoard: number = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] >= target) {
// 左边界一定在mid左边(不含mid)
right = mid - 1;
leftBoard = right;
} else {
// 左边界在mid右边(含mid)
left = mid + 1;
}
}
return leftBoard;
}
```


### Scala
```scala
object Solution {
Expand Down Expand Up @@ -527,5 +583,6 @@ object Solution {
}
```


-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
29 changes: 29 additions & 0 deletions problems/0235.二叉搜索树的最近公共祖先.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,36 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```

## Scala

递归:

```scala
object Solution {
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
// scala中每个关键字都有其返回值,于是可以不写return
if (root.value > p.value && root.value > q.value) lowestCommonAncestor(root.left, p, q)
else if (root.value < p.value && root.value < q.value) lowestCommonAncestor(root.right, p, q)
else root
}
}
```

迭代:

```scala
object Solution {
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
var curNode = root // 因为root是不可变量,所以要赋值给curNode一个可变量
while(curNode != null){
if(curNode.value > p.value && curNode.value > q.value) curNode = curNode.left
else if(curNode.value < p.value && curNode.value < q.value) curNode = curNode.right
else return curNode
}
null
}
}
```


-----------------------
Expand Down
18 changes: 18 additions & 0 deletions problems/0236.二叉树的最近公共祖先.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,25 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```

## Scala

```scala
object Solution {
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
// 递归结束条件
if (root == null || root == p || root == q) {
return root
}

var left = lowestCommonAncestor(root.left, p, q)
var right = lowestCommonAncestor(root.right, p, q)

if (left != null && right != null) return root
if (left == null) return right
left
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
28 changes: 28 additions & 0 deletions problems/0450.删除二叉搜索树中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,35 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
};
```

## Scala

```scala
object Solution {
def deleteNode(root: TreeNode, key: Int): TreeNode = {
if (root == null) return root // 第一种情况,没找到删除的节点,遍历到空节点直接返回
if (root.value == key) {
// 第二种情况: 左右孩子都为空,直接删除节点,返回null
if (root.left == null && root.right == null) return null
// 第三种情况: 左孩子为空,右孩子不为空,右孩子补位
else if (root.left == null && root.right != null) return root.right
// 第四种情况: 左孩子不为空,右孩子为空,左孩子补位
else if (root.left != null && root.right == null) return root.left
// 第五种情况: 左右孩子都不为空,将删除节点的左子树头节点(左孩子)放到
// 右子树的最左边节点的左孩子上,返回删除节点的右孩子
else {
var tmp = root.right
while (tmp.left != null) tmp = tmp.left
tmp.left = root.left
return root.right
}
}
if (root.value > key) root.left = deleteNode(root.left, key)
if (root.value < key) root.right = deleteNode(root.right, key)

root // 返回根节点,return关键字可以省略
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
37 changes: 37 additions & 0 deletions problems/0701.二叉搜索树中的插入操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,43 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
```


## Scala

递归:

```scala
object Solution {
def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
if (root == null) return new TreeNode(`val`)
if (`val` < root.value) root.left = insertIntoBST(root.left, `val`)
else root.right = insertIntoBST(root.right, `val`)
root // 返回根节点
}
}
```

迭代:

```scala
object Solution {
def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
if (root == null) {
return new TreeNode(`val`)
}
var parent = root // 记录当前节点的父节点
var curNode = root
while (curNode != null) {
parent = curNode
if(`val` < curNode.value) curNode = curNode.left
else curNode = curNode.right
}
if(`val` < parent.value) parent.left = new TreeNode(`val`)
else parent.right = new TreeNode(`val`)
root // 最终返回根节点
}
}
```


-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>