# Given a binary tree # # # struct Node { # int val; # Node *left; # Node *right; # Node *next; # } # # # Populate each next pointer to point to its next right node. If there is no ne # xt right node, the next pointer should be set to NULL. # # Initially, all next pointers are set to NULL. # # # # Follow up: # # # You may only use constant extra space. # Recursive approach is fine, you may assume implicit stack space does not coun # t as extra space for this problem. # # # # Example 1: # # # # # Input: root = [1,2,3,4,5,null,7] # Output: [1,#,2,3,#,4,5,7,#] # Explanation: Given the above binary tree (Figure A), your function should popu # late each next pointer to point to its next right node, just like in Figure B. T # he serialized output is in level order as connected by the next pointers, with ' # #' signifying the end of each level. # # # # Constraints: # # # The number of nodes in the given tree is less than 6000. # -100 <= node.val <= 100 # # Related Topics 树 深度优先搜索 # 👍 263 👎 0
# leetcode submit region begin(Prohibit modification and deletion) classSolution: defconnect(self, root: 'Node') -> 'Node': ifnot root: returnNone list = [root] while len(list) > 0: newList = [] if list[0].left : newList.append(list[0].left) if list[0].right : newList.append(list[0].right) for i in range(1, len(list)): list[i-1].next = list[i] if list[i].left : newList.append(list[i].left) if list[i].right : newList.append(list[i].right) list = newList return root # leetcode submit region end(Prohibit modification and deletion)
# leetcode submit region begin(Prohibit modification and deletion) classSolution: defconnect(self, root: 'Node') -> 'Node': ifnot root: returnNone leftmost = root # 第n层最左节点 while leftmost: head = Node(0) # 第n+1层的虚拟头结点 pre = head # 第n+1层的当前节点 cur = leftmost # 第n层当前节点 while cur: if cur.left: pre.next = cur.left pre = cur.left if cur.right: pre.next = cur.right pre = cur.right cur = cur.next leftmost = head.next # leftmost指向第n+1层最左节点 return root # leetcode submit region end(Prohibit modification and deletion)