leetcode刷题笔记一百一十六题与一百一十七题 填充每个节点的下一个右侧节点指针 与 填充每个节点的下一个右侧节点指针 II
leetcode刷题笔记一百一十六题与一百一十七题 填充每个节点的下一个右侧节点指针 与 填充每个节点的下一个右侧节点指针 II
问题描述:
116题问题描述
给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。
输入:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}
输出:{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。
117题问题描述
给定一个二叉树
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。
进阶:
你只能使用常量级额外空间。
使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。输入:root = [1,2,3,4,5,null,7]
输出:[1,#,2,3,#,4,5,7,#]
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。提示:
树中的节点数小于 6000
-100 <= node.val <= 100
/**
* 116题与117题的主要区别在于116题的树是完美二叉树,117题的树是普通二叉树,处理这类题的思想基本是一致的。1.采用层次遍历,遍历过程中修改next指针,或者在上一层遍历时建立下一层的next指针关系,逐层解决
*/
//基于层次遍历的解法
/**
* Definition for a Node.
* class Node(var _value: Int) {
* var value: Int = _value
* var left: Node = null
* var right: Node = null
* var next: Node = null
* }
*/
import scala.collection.mutable
object Solution {
def connect(root: Node): Node = {
if (root == null) return root
val queue = mutable.Queue[Node]()
queue.enqueue(root)
while (queue.isEmpty == false){
val queueSize = queue.size
//到本次倒数第二个结点,全部更新next指针
for (i <- 0 to queueSize-2){
val currNode = queue.dequeue
val nextNode = queue.head
currNode.next = nextNode
//排除非空的情况
if (currNode.left != null) queue.enqueue(currNode.left)
if (currNode.right != null) queue.enqueue(currNode.right)
}
//更新最后一个结点的next指针与入队情况
val floorLastNode = queue.dequeue
floorLastNode.next = null
if (floorLastNode.left != null) queue.enqueue(floorLastNode.left)
if (floorLastNode.right != null) queue.enqueue(floorLastNode.right)
}
return root
}
}
//使用已建立的 next 指针方法,本质上利用上层遍历更新下层的next指针
//116题
/**
* Definition for a Node.
* class Node(var _value: Int) {
* var value: Int = _value
* var left: Node = null
* var right: Node = null
* var next: Node = null
* }
*/
object Solution {
def connect(root: Node): Node = {
if (root == null) return root
//始终从每层的左侧结点开始
var leftMost = root
while (leftMost.left != null){
//使用head遍历整层结点
var head = leftMost
while (head != null){
//由于是完美二叉树,head不为空,将左儿子的next指向右儿子
head.left.next = head.right
//不同父节点的相邻节点,将右子树的next指向存在的兄弟节点的左儿子
if (head.next != null) head.right.next = head.next.left
//更新head结点
head = head.next
}
leftMost = leftMost.left
}
return root
}
}
//117题
object Solution {
def connect(root: Node): Node = {
var parent = root
val head = new Node(0)
while (parent != null){
//当前层
var curr = head
while (parent != null){
//检查左子树情况,有就更新next
if (parent.left != null){
curr.next = parent.left
curr = curr.next
}
//检查左子树情况,有就更新next
if (parent.right != null){
curr.next = parent.right
curr = curr.next
}
//层内更新
parent = parent.next
}
//前往下一层
parent = head.next
head.next = null
}
return root
}
}