Skip to content

Commit beeca1c

Browse files
feat: add solutions to lc problems: No.1791, 0997, 0590, 0589, 0099 (doocs#528)
* Updated Operator * Added Python Solution to Problem No.590 to README_EN.md * feat: add python solution to lc problem: No.0590. N-ary Tree Postorder Traversa * Added Python solution No.590 to README.md * feat: add python solution to lc problem: No. 0099. Recover Binary Search * Added Python Solution NO.0099 to README.md * Added Python Solution to No.0099 README_EN.md * feat: add python solution to lc problem: No. 0997.Find the Town Judge * Added Python Solution to No.0997 README.md * Added Python Solution to No.0997 README_EN.md * feat: add python solution to lc problem: No.1791. Find Center of StarGraph * Added Python solution to No.1791 README.md * Added Python Solution to NO.1791 README_EN.md * feat: add python solution to lc problem: No.0589 N-ary Tree Preorder Traversal * Added Python Solution to No.0589 README.md * Added Python Solution to No.0589 README_EN.md * Update solution/0500-0599/0590.N-ary Tree Postorder Traversal/README.md * Update solution/0000-0099/0035.Search Insert Position/Solution.py * Update solution/0500-0599/0590.N-ary Tree Postorder Traversal/README_EN.md Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 9bd1922 commit beeca1c

File tree

15 files changed

+256
-10
lines changed

15 files changed

+256
-10
lines changed

solution/0000-0099/0099.Recover Binary Search Tree/README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,31 @@
4848
<!-- 这里可写当前语言的特殊实现逻辑 -->
4949

5050
```python
51-
51+
# Definition for a binary tree node.
52+
# class TreeNode:
53+
# def __init__(self, val=0, left=None, right=None):
54+
# self.val = val
55+
# self.left = left
56+
# self.right = right
57+
class Solution:
58+
first = None
59+
second = None
60+
prev = None
61+
def recoverTree(self, root: TreeNode) -> None:
62+
"""
63+
Do not return anything, modify root in-place instead.
64+
"""
65+
def dfs(root):
66+
if root:
67+
dfs(root.left)
68+
if self.prev and root.val < self.prev.val:
69+
if not self.first:
70+
self.first = self.prev
71+
self.second = root
72+
self.prev = root
73+
dfs(root.right)
74+
dfs(root)
75+
self.first.val, self.second.val = self.second.val, self.first.val
5276
```
5377

5478
### **Java**

solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,31 @@
4141
### **Python3**
4242

4343
```python
44-
44+
# Definition for a binary tree node.
45+
# class TreeNode:
46+
# def __init__(self, val=0, left=None, right=None):
47+
# self.val = val
48+
# self.left = left
49+
# self.right = right
50+
class Solution:
51+
first = None
52+
second = None
53+
prev = None
54+
def recoverTree(self, root: TreeNode) -> None:
55+
"""
56+
Do not return anything, modify root in-place instead.
57+
"""
58+
def dfs(root):
59+
if root:
60+
dfs(root.left)
61+
if self.prev and root.val < self.prev.val:
62+
if not self.first:
63+
self.first = self.prev
64+
self.second = root
65+
self.prev = root
66+
dfs(root.right)
67+
dfs(root)
68+
self.first.val, self.second.val = self.second.val, self.first.val
4569
```
4670

4771
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
first = None
9+
second = None
10+
prev = None
11+
def recoverTree(self, root: TreeNode) -> None:
12+
"""
13+
Do not return anything, modify root in-place instead.
14+
"""
15+
def dfs(root):
16+
if root:
17+
dfs(root.left)
18+
if self.prev and root.val < self.prev.val:
19+
if not self.first:
20+
self.first = self.prev
21+
self.second = root
22+
self.prev = root
23+
dfs(root.right)
24+
dfs(root)
25+
self.first.val, self.second.val = self.second.val, self.first.val

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,25 @@
6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

6262
```python
63-
63+
"""
64+
# Definition for a Node.
65+
class Node:
66+
def __init__(self, val=None, children=None):
67+
self.val = val
68+
self.children = children
69+
"""
70+
71+
class Solution:
72+
def preorder(self, root: 'Node') -> List[int]:
73+
if not root:
74+
return []
75+
def PO(root):
76+
res.append(root.val)
77+
for i in root.children:
78+
PO(i)
79+
res=[]
80+
PO(root)
81+
return res
6482
```
6583

6684
### **Java**

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@
4747
### **Python3**
4848

4949
```python
50-
50+
"""
51+
# Definition for a Node.
52+
class Node:
53+
def __init__(self, val=None, children=None):
54+
self.val = val
55+
self.children = children
56+
"""
57+
58+
class Solution:
59+
def preorder(self, root: 'Node') -> List[int]:
60+
if not root:
61+
return []
62+
def PO(root):
63+
res.append(root.val)
64+
for i in root.children:
65+
PO(i)
66+
res=[]
67+
PO(root)
68+
return res
5169
```
5270

5371
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, children=None):
5+
self.val = val
6+
self.children = children
7+
"""
8+
9+
class Solution:
10+
def preorder(self, root: 'Node') -> List[int]:
11+
if not root:
12+
return []
13+
def PO(root):
14+
res.append(root.val)
15+
for i in root.children:
16+
PO(i)
17+
res=[]
18+
PO(root)
19+
return res

solution/0500-0599/0590.N-ary Tree Postorder Traversal/README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,28 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
"""
65+
# Definition for a Node.
66+
class Node:
67+
def __init__(self, val=None, children=None):
68+
self.val = val
69+
self.children = children
70+
"""
71+
class Solution:
72+
def postorder(self, root: 'Node') -> List[int]:
73+
if not root:
74+
return []
75+
def PO(root):
76+
if root==None:
77+
return res
78+
else:
79+
for i in root.children:
80+
PO(i)
81+
res.append(i.val)
82+
res=[]
83+
PO(root)
84+
res.append(root.val)
85+
return res
6586
```
6687

6788
### **Java**

solution/0500-0599/0590.N-ary Tree Postorder Traversal/README_EN.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,29 @@
4343
### **Python3**
4444

4545
```python
46-
46+
"""
47+
# Definition for a Node.
48+
class Node:
49+
def __init__(self, val=None, children=None):
50+
self.val = val
51+
self.children = children
52+
"""
53+
54+
class Solution:
55+
def postorder(self, root: 'Node') -> List[int]:
56+
if not root:
57+
return []
58+
def PO(root):
59+
if root==None:
60+
return res
61+
else:
62+
for i in root.children:
63+
PO(i)
64+
res.append(i.val)
65+
res=[]
66+
PO(root)
67+
res.append(root.val)
68+
return res
4769
```
4870

4971
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, children=None):
5+
self.val = val
6+
self.children = children
7+
"""
8+
9+
class Solution:
10+
def postorder(self, root: 'Node') -> List[int]:
11+
if not root:
12+
return []
13+
def PO(root):
14+
if root==None:
15+
return res
16+
else:
17+
for i in root.children:
18+
PO(i)
19+
res.append(i.val)
20+
res=[]
21+
PO(root)
22+
res.append(root.val)
23+
return res

solution/0900-0999/0997.Find the Town Judge/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,22 @@
7575
<!-- 这里可写当前语言的特殊实现逻辑 -->
7676

7777
```python
78-
78+
class Solution:
79+
def findJudge(self, n: int, trust: List[List[int]]) -> int:
80+
if n == 1 and len(trust) == 0:
81+
return 1
82+
dic = {}
83+
values = set()
84+
for i in trust:
85+
values.add(i[0])
86+
if i[1] in dic:
87+
dic[i[1]].append(i[0])
88+
else: dic[i[1]] = [i[0]]
89+
90+
for key, value in dic.items():
91+
if len(dic[key]) == n-1 and key not in values:
92+
return key
93+
return -1
7994
```
8095

8196
### **Java**

solution/0900-0999/0997.Find the Town Judge/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,22 @@
8484
### **Python3**
8585

8686
```python
87-
87+
class Solution:
88+
def findJudge(self, n: int, trust: List[List[int]]) -> int:
89+
if n == 1 and len(trust) == 0:
90+
return 1
91+
dic = {}
92+
values = set()
93+
for i in trust:
94+
values.add(i[0])
95+
if i[1] in dic:
96+
dic[i[1]].append(i[0])
97+
else: dic[i[1]] = [i[0]]
98+
99+
for key, value in dic.items():
100+
if len(dic[key]) == n-1 and key not in values:
101+
return key
102+
return -1
88103
```
89104

90105
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def findJudge(self, n: int, trust: List[List[int]]) -> int:
3+
if n == 1 and len(trust) == 0:
4+
return 1
5+
dic = {}
6+
values = set()
7+
for i in trust:
8+
values.add(i[0])
9+
if i[1] in dic:
10+
dic[i[1]].append(i[0])
11+
else: dic[i[1]] = [i[0]]
12+
13+
for key, value in dic.items():
14+
if len(dic[key]) == n-1 and key not in values:
15+
return k

solution/1700-1799/1791.Find Center of Star Graph/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
<!-- 这里可写当前语言的特殊实现逻辑 -->
5353

5454
```python
55-
55+
class Solution:
56+
def findCenter(self, edges: List[List[int]]) -> int:
57+
return edges[0][0] if edges[0][0]==edges[1][0] or edges[0][0]==edges[1][1] else edges[0][1]
5658
```
5759

5860
### **Java**

solution/1700-1799/1791.Find Center of Star Graph/README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
### **Python3**
6767

6868
```python
69-
69+
class Solution:
70+
def findCenter(self, edges: List[List[int]]) -> int:
71+
return edges[0][0] if edges[0][0]==edges[1][0] or edges[0][0]==edges[1][1] else edges[0][1]
7072
```
7173

7274
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def findCenter(self, edges: List[List[int]]) -> int:
3+
return edges[0][0] if edges[0][0]==edges[1][0] or edges[0][0]==edges[1][1] else edges[0][1]

0 commit comments

Comments
 (0)