码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode116 Populating Next Right Pointers in Each Node

时间:2020-02-28 01:33:59      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:nod   roo   solution   class   tin   tco   leetcode   pointer   规律   

 1 """
 2 首先理解题意,因为要找结点的下个指针
 3 不能从根节点来看。要从第二层看来判断
 4 找到规律自然用递归写法
 5 """
 6 class Node:
 7     def __init__(self, val: int = 0, left: Node = None, right: Node = None, next: Node = None):
 8         self.val = val
 9         self.left = left
10         self.right = right
11         self.next = next
12 
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

 

leetcode116 Populating Next Right Pointers in Each Node

标签:nod   roo   solution   class   tin   tco   leetcode   pointer   规律   

原文地址:https://www.cnblogs.com/yawenw/p/12375449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!