-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117.py
More file actions
26 lines (24 loc) · 830 Bytes
/
Copy path117.py
File metadata and controls
26 lines (24 loc) · 830 Bytes
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
# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
if root is not None:
self.parseLevel([root])
def parseLevel(self, parselist):
for i in range(len(parselist) - 1):
parselist[i].next = parselist[i + 1]
nextlist = []
for i in range(len(parselist)):
if parselist[i].left is not None:
nextlist.append(parselist[i].left)
if parselist[i].right is not None:
nextlist.append(parselist[i].right)
if len(nextlist) != 0:
self.parseLevel(nextlist)