LeetCode 0116. 填充每个节点的下一个右侧节点指针【Python】【Go】

Problem

LeetCode

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

1struct Node { 2 int val; 3 Node *left; 4 Node *right; 5 Node *next; 6}

Populate each next pointer to point to its next right node. If there is no next 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 count as extra space for this problem.

Example 1:

1Input: root = [1,2,3,4,5,6,7] 2Output: [1,#,2,3,#,4,5,6,7,#] 3Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

问题

力扣

给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

1struct Node { 2 int val; 3 Node *left; 4 Node *right; 5 Node *next; 6}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。

初始状态下,所有 next 指针都被设置为 NULL。

进阶:

  • 你只能使用常量级额外空间。
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。

示例:

1输入:root = [1,2,3,4,5,6,7] 2输出:[1,#,2,3,#,4,5,6,7,#] 3解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。

提示:

  • 树中节点的数量少于 4096
  • -1000 <= node.val <= 1000

思路

递归

通过构造辅助函数,将每一层节点连接起来转化成将每两个相邻节点连接起来。

Python3 代码

1""" 2# Definition for a Node. 3class Node: 4 def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): 5 self.val = val 6 self.left = left 7 self.right = right 8 self.next = next 9""" 10 11class Solution: 12 def connect(self, root: 'Node') -> 'Node': 13 if not root: 14 return None 15 16 # 将每两个相邻节点连接起来 17 def connectTwoNode(node1: 'Node', node2: 'Node'): 18 if not node1 or not node2: 19 return 20 21 node1.next = node2 22 23 # 链接相同父节点的两个子节点 24 connectTwoNode(node1.left, node1.right) 25 connectTwoNode(node2.left, node2.right) 26 # 链接不同父节点的两个子节点 27 connectTwoNode(node1.right, node2.left) 28 29 connectTwoNode(root.left, root.right) 30 return root

Go 代码

1/** 2 * Definition for a Node. 3 * type Node struct { 4 * Val int 5 * Left *Node 6 * Right *Node 7 * Next *Node 8 * } 9 */ 10 11 12 // 将每两个相邻节点连接起来 13 func connectTwoNode(node1 *Node, node2 *Node) { 14 if node1 == nil && node2 == nil { 15 return 16 } 17 node1.Next = node2 18 19 // 链接相同父节点的两个子节点 20 connectTwoNode(node1.Left, node1.Right) 21 connectTwoNode(node2.Left, node2.Right) 22 // 链接不同父节点的两个子节点 23 connectTwoNode(node1.Right, node2.Left) 24 } 25 26 func connect(root *Node) *Node { 27 if root == nil { 28 return nil 29 } 30 31 connectTwoNode(root.Left, root.Right) 32 return root 33}

GitHub 链接

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )