Skip to content

Commit af6e2d3

Browse files
committed
feat: 701 993
1 parent 5e64a4f commit af6e2d3

File tree

8 files changed

+258
-3
lines changed

8 files changed

+258
-3
lines changed

.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
{
88
"type": "node",
99
"request": "launch",
10-
"name": "nodemon",
11-
"runtimeExecutable": "nodemon",
12-
"program": "${fileDirname}",
10+
"name": "node",
11+
"runtimeExecutable": "node",
12+
"program": "${fileDirname}/index.js",
1313
"restart": true,
1414
"console": "integratedTerminal",
1515
"internalConsoleOptions": "neverOpen"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [701. Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/)
2+
3+
## 2019/08/21
4+
5+
### 题目 💗[medium]
6+
7+
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
8+
9+
Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
10+
11+
For example,
12+
13+
```bash
14+
Given the tree:
15+
4
16+
/ \
17+
2 7
18+
/ \
19+
1 3
20+
And the value to insert: 5
21+
```
22+
23+
You can return this binary search tree:
24+
25+
```bash
26+
4
27+
/ \
28+
2 7
29+
/ \ /
30+
1 3 5
31+
```
32+
33+
This tree is also valid:
34+
35+
```bash
36+
5
37+
/ \
38+
2 7
39+
/ \
40+
1 3
41+
\
42+
4
43+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package insertIntoBST
2+
3+
import "github.com/pengliheng/leetcode/Helper"
4+
5+
type TreeNode = Helper.TreeNode
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
16+
if root == nil {
17+
return &TreeNode{Val: val}
18+
}
19+
if root.Val > val {
20+
root.Left = insertIntoBST(root.Left, val)
21+
} else {
22+
root.Right = insertIntoBST(root.Right, val)
23+
}
24+
return root
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package insertIntoBST
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *TreeNode
13+
N2 int
14+
ans *TreeNode
15+
}{
16+
{
17+
Helper.Ints2TreeNode([]int{4, 2, 7, 1, 3}),
18+
5,
19+
Helper.Ints2TreeNode([]int{4, 2, 7, 1, 3, 5}),
20+
},
21+
}
22+
23+
func Test_bitwiseComplement(t *testing.T) {
24+
ast := assert.New(t)
25+
for _, tc := range tcs {
26+
ast.Equal(tc.ans, insertIntoBST(tc.N1, tc.N2), "输入:%v", tc)
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [993. Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)
2+
3+
## 2019/08/21
4+
5+
### 题目 💗[medium]
6+
7+
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
8+
9+
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
10+
11+
We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
12+
13+
Return true if and only if the nodes corresponding to the values x and y are cousins.
14+
15+
```bash
16+
Example 1:
17+
18+
Input: root = [1,2,3,4], x = 4, y = 3
19+
Output: false
20+
```
21+
22+
```bash
23+
Example 2:
24+
25+
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
26+
Output: true
27+
```
28+
29+
```bash
30+
Example 3:
31+
32+
Input: root = [1,2,3,null,4], x = 2, y = 3
33+
Output: false
34+
```
35+
36+
Note:
37+
38+
- The number of nodes in the tree will be between 2 and 100.
39+
- Each node has a unique integer value from 1 to 100.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package isCousins
2+
3+
import "github.com/pengliheng/leetcode/Helper"
4+
5+
type TreeNode = Helper.TreeNode
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
func isCousins(root *TreeNode, x int, y int) bool {
16+
xx := -1
17+
xxx := -1
18+
yy := -2
19+
yyy := -2
20+
var helper func(*TreeNode, int, int)
21+
helper = func(root *TreeNode, depth int, parent int) {
22+
if root == nil {
23+
return
24+
}
25+
if xx < 0 && root.Val == x {
26+
xx = depth
27+
xxx = parent
28+
} else if yy < 0 && root.Val == y {
29+
yy = depth
30+
yyy = parent
31+
}
32+
helper(root.Left, depth+1, root.Val)
33+
helper(root.Right, depth+1, root.Val)
34+
}
35+
helper(root, 0, -3)
36+
return xx == yy && xxx != yyy
37+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package isCousins
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *TreeNode
13+
N2 int
14+
N3 int
15+
ans bool
16+
}{
17+
// {
18+
// Helper.Ints2TreeNode([]int{1, 2, 3, -1 << 63, 4, -1 << 63, 5}),
19+
// 5,
20+
// 4,
21+
// true,
22+
// },
23+
{
24+
Helper.Ints2TreeNode([]int{1, 2, 3, -1 << 63, 4}),
25+
2,
26+
3,
27+
false,
28+
},
29+
// {
30+
// Helper.Ints2TreeNode([]int{1, -1 << 63, 2, 3}),
31+
// 4,
32+
// 3,
33+
// false,
34+
// },
35+
}
36+
37+
func Test_bitwiseComplement(t *testing.T) {
38+
ast := assert.New(t)
39+
for _, tc := range tcs {
40+
ast.Equal(tc.ans, isCousins(tc.N1, tc.N2, tc.N3), "输入:%v", tc)
41+
}
42+
}

index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var maxDepth = function (root) {
2+
if (root.children.length < 1) return 0;
3+
return 1 + max(root.children.map(e=>maxDepth(e)))
4+
};
5+
6+
const max = (arr) => {
7+
var res = arr[0]
8+
arr.forEach(e => {
9+
if (e > res) {
10+
res = e
11+
}
12+
})
13+
return res
14+
}
15+
16+
const res = maxDepth({
17+
"$id": "1",
18+
"children": [{
19+
"$id": "2",
20+
"children": [{
21+
"$id": "5",
22+
"children": [],
23+
"val": 5
24+
}, {
25+
"$id": "6",
26+
"children": [],
27+
"val": 6
28+
}],
29+
"val": 3
30+
}, {
31+
"$id": "3",
32+
"children": [],
33+
"val": 2
34+
}, {
35+
"$id": "4",
36+
"children": [],
37+
"val": 4
38+
}],
39+
"val": 1
40+
})
41+
console.log(res);

0 commit comments

Comments
 (0)