-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0987.py
More file actions
67 lines (56 loc) · 2.1 KB
/
q0987.py
File metadata and controls
67 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
from collections import deque
from typing import List
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
dic = {}
def verticalTraversal_helper(self, root: TreeNode, xIndex:int, yIndex:int):
if not root:
return
if xIndex not in self.dic:
self.dic[xIndex] = {}
if yIndex not in self.dic[xIndex]:
self.dic[xIndex][yIndex] = list()
self.dic[xIndex][yIndex].append(root.val)
self.verticalTraversal_helper(root.left, xIndex - 1, yIndex + 1)
self.verticalTraversal_helper(root.right, xIndex + 1, yIndex + 1)
def verticalTraversal(self, root: TreeNode) -> List[List[int]]:
self.dic = {}
self.verticalTraversal_helper(root, 0, 0)
keys = sorted(self.dic.keys())
result = list()
for key in keys:
temp = list()
for y in sorted(self.dic[key].keys()):
for number in sorted(self.dic[key][y]):
temp.append(number)
result.append(temp)
return result
# 垃圾题目,限制
# If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
# 用不了bfs
def verticalTraversal_bfs(self, root: TreeNode) -> List[List[int]]:
self.dic = {}
bfsNode = deque([root])
bfsXIndex = deque([0])
while bfsNode:
node = bfsNode.popleft()
xIndex = bfsXIndex.popleft()
if node:
if xIndex not in self.dic:
self.dic[xIndex] = list()
self.dic[xIndex].append(node.val)
bfsNode.append(node.left)
bfsXIndex.append(xIndex - 1)
bfsNode.append(node.right)
bfsXIndex.append(xIndex + 1)
keys = sorted(self.dic.keys())
result = list()
for key in keys:
result.append(self.dic[key].copy())
return result