leetcode116 Populating Next Right Pointers in Each Node

1  "" " 
2  first understanding the problem, because the next node pointer looking
 3  . The second layer can not be determined from the root term opinion from
 4  to find the natural law recursively written
 . 5  " "" 
. 6  class the Node:
 . 7      DEF  the __init__ (Self, Val: int = 0, left: ' the Node ' = None, right: ' the Node ' = None, Next: ' the Node ' = None):
 . 8          self.val = Val
 . 9          self.left = left
 10          = self.right right
 . 11          self.next = Next
 12 is 
13 class Solution:
14     def connect(self, root: 'Node') -> 'Node':
15         if not root:
16             return None
17         if root.left:
18             root.left.next = root.right
19         if root.right:
20             root.right.next = root.next.left if root.next else None #!!!从第二层来判断
21         self.connect(root.left)
22         self.connect(root.right)
23         return root

 

Guess you like

Origin www.cnblogs.com/yawenw/p/12375449.html