117.填充每个节点的下一个右侧结点指针Ⅱ

1.题目描述

给定一个二叉树:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

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

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

示例 1:

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

示例 2:

输入:root = []
输出:[]

提示:

  • 树中的节点数在范围 [0, 6000] 内
  • -100 <= Node.val <= 100

进阶:

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

2.解题思路

2.1 层序遍历-BFS

借助队列进行层序遍历,在遍历的过程中,给当前层的元素next域赋值,同时如果当前遍历元素有left/right孩子时,再将下层结点入队,当队中元素为空时,就完成了所有next域的赋值。

2.2 递归解法-DFS

定义一个函数getNext(Node node)用于寻找下一个结点;

在遍历root的过程中,我们要处理root的左右孩子的next域,存在以下几种情况:

  1. root的左右孩子都不为空,那么root.left.next = root.right
  2. root的左孩子不为空但右孩子为空,那么root.left.next = getNext(root.next),因为右孩子为空,所以要到当前root的兄弟/堂兄弟结点中去找左孩子的next
  3. root的右孩子不为空时,需要找右孩子的next,同上,它的root.right.next = getNext(root.next)
  4. 当处理完root的左右孩子的next域后,需要递归的寻找右子树和左子树

注:递归时,一定要先处理右子树,再处理左子树,因为如果先处理左子树会有节点的next域明明有下一个结点,但却被置为null,这样就会使的连接的过程中丢失结点,如果先处理右子树,在getNext(root.next)时,当前的root.next一定是已经指向了堂兄弟的,所以getNext能找到正确的结果

3.代码实现

3.1 思路1

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if (root == null) return root;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int sz = queue.size();
            for (int i = 0; i < sz; i++) {
                Node cur = queue.poll();
                if (i == sz - 1) {
                    cur.next = null;
                } else {
                    cur.next = queue.peek();
                }
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
        }
        return root;
    }
}

3.2 思路2

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if (root == null) {
            return root;
        }
        //如果当前结点左右孩子都不为空,那么左孩子的next就是右孩子
        if (root.left != null && root.right != null) {
            root.left.next = root.right;
        }
        //如果左不空,右空,左孩子的next就通过getNext(root.next)找root的兄弟结点的孩子
        if (root.left != null && root.right == null) {
            root.left.next = getNext(root.next);
        }
        //如果右不空,还要给右孩子找next域
        if (root.right != null) {
            root.right.next = getNext(root.next);
        }
        //当前结点的左右孩子都处理完毕,需要递归的处理以左右孩子为根的子树
        connect(root.right);
        connect(root.left);
        return root;
    }
    
    public Node getNext(Node node) {
        if (node == null) {
            return null;
        }
        if (node.left != null) {
            return node.left;
        }
        if (node.right != null) {
            return node.right;
        }
        if (node.next != null) {
            return getNext(node.next);
        }
        return null;
    }
}
创建一个包含n个节点的链表通常涉及到以下几个步骤,这里以单向链表为例: 1. **定义节点结构**: 首先,你需要定义一个存储数据的节点结构体(Node),它通常包含两个成员:一个是数据域(data),另一个是指向下一个节点指针(next)。 ```c typedef struct Node { int data; // 节点的数据 struct Node* next; // 指向下一个节点指针 } Node; ``` 2. **初始化链表头**: 创建一个结点(head),用于链接所有节点。如果链表为空(n=0),则head应指向NULL。 ```c Node* head = NULL; ``` 3. **添加节点**: 你可以编写一个函数(如`addNode`)来插入新的节点到链表中。对于第i个节点,其需要链接到当前节点的`next`并更新`head`。 ```c void addNode(Node** head, int n, int data) { if (n == 0) { new Node{data, NULL}; // 如果链表空,创建第一个节点 *head = *head = &newNode; } else { Node* newNode = malloc(sizeof(Node)); // 动态分配内存 newNode->data = data; newNode->next = (*head); // 将新节点的next指向当前头结点 *head = newNode; // 更新头结点 // 对于其他节点,递归地在新节点上操作 for (int i = 1; i < n - 1; ++i) { newNode = malloc(sizeof(Node)); newNode->data = ...; // 根据需求填充数据 newNode->next = (*head)->next; (*head)->next = newNode; *head = newNode; // 更新头结点指向最后一个已添加节点 } } } ``` 4. **遍历链表**: 为了访问链表中的每个节点,可以编写一个循环遍历函数,从头结点开始逐个节点访问。 ```c void traverseList(Node* head) { if (head != NULL) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } else { printf("链表为空\n"); } } ``` 5. **释放内存**: 当不再需要链表时,别忘了删除节点以避免内存泄漏。这通常通过一个`free`函数完成,从头结点开始逐个释放节点。 ```c void deleteList(Node** head) { Node* temp = *head; Node* next; while (temp != NULL) { next = temp->next; free(temp); // 释放当前节点 temp = next; } *head = NULL; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值