LeetCode 116: Binary tree fills the next right node pointer of each node

The problem is as follows:
Given a perfect binary tree, all its leaf nodes are at the same level, and each parent node has two child nodes. The binary tree is defined as follows:

struct Node { int val; Node *left; Node *right; Node *next; } Fill each of its next pointers so that this pointer points to its next right node. If the next right node cannot be found, the next pointer is set to NULL.





In the initial state, all next pointers are set to NULL.

Insert picture description here

输入:{
    
    "$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 所示。

Tip:
You can only use constant-level extra space.
Using recursion to solve the problem also meets the requirements. The stack space occupied by the recursive program in this problem is not counted as additional space complexity.

For this topic, we can easily think of the following situation:
1. Do the same operation for each node.

Node connect(Node root) {
    
    
    if (root == null || root.left == null) {
    
    
        return root;
    }

    root.left.next = root.right;

    connect(root.left);
    connect(root.right);

    return root;
}

Then there is the following result:
Insert picture description here
Is there any difference found? Yes, the left and right ends of the different parent nodes are not connected together. Since it is impossible to operate on one node, consider operating on two nodes:

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

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    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 NULL;
        connectTwoNode(root->left,root->right); //连接两个相邻的结点
        return root;
    }

    void connectTwoNode(Node *a,Node *b){
    
    
        if(a==NULL||b==NULL) return;

        a->next=b;
        /*将相同父节点的两点连接*/
        connectTwoNode(a->left,a->right);
        connectTwoNode(b->left,b->right);
        /*不同父节点的两点连接*/
        connectTwoNode(a->right,b->left);
    }

};

This is done. Remember to deal with the binary tree. To figure out whether the same action can be performed on each combination consistently, this combination can also be a single node.

  • [Reference public account: labuladong]

Guess you like

Origin blog.csdn.net/weixin_45146520/article/details/109056263