📜  连接同级节点

📅  最后修改于: 2022-05-13 01:57:17.887000             🧑  作者: Mango

连接同级节点

编写一个函数,将二叉树中同一级别的所有相邻节点连接起来。给定二叉树节点的结构如下所示。

C++
struct node {
    int data;
    struct node* left;
    struct node* right;
    struct node* nextRight;
}


C
struct node {
    int data;
    struct node* left;
    struct node* right;
    struct node* nextRight;
}


Javascript
class node {
 
    constructor()
    {
       this.data = 0;
       this.left = null;
       this.right = null;
       this.nextRight = null;
     }
}
 
// This code is contributed by importantly.


C++
/* Iterative program to connect all the adjacent nodes at the same level in a binary tree*/
#include 
#include
using namespace std;
 
// A Binary Tree Node
class node {
public:
    int data;
    node* left;
    node* right;
    node* nextRight;
  
    /* Constructor that allocates a new node with the
    given data and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
        this->nextRight = NULL;
    }
};
// setting right pointer to next right node
/*
             10 ----------> NULL
            /  \
          8 --->2 --------> NULL
         /
        3 ----------------> NULL
        */
void connect(node *root)
    {
       //Base condition
       if(root==NULL)
        return;
       // Create an empty queue like level order traversal
       queue q;
       q.push(root);
       while(!q.empty()){
         // size indicates no. of nodes at current level
           int size=q.size();
         // for keeping track of previous node
           node* prev=NULL;
           while(size--){
               node* temp=q.front();
               q.pop();
              
               if(temp->left)
                q.push(temp->left);
              
               if(temp->right)
                q.push(temp->right);
               
               if(prev!=NULL)
                prev->nextRight=temp;
               prev=temp;
           }
           prev->nextRight=NULL;
       }
    }
 
int main() {
      /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
    // Let us create binary tree shown above
    node* root = new node(10);
    root->left = new node(8);
    root->right = new node(2);
    root->left->left = new node(3);
    connect(root);
    // Let us check the values
    // of nextRight pointers
    cout << "Following are populated nextRight pointers in the tree"
            " (-1 is printed if there is no nextRight)\n";
    cout << "nextRight of " << root->data << " is "
         << (root->nextRight ? root->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->data << " is "
         << (root->left->nextRight ? root->left->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->right->data << " is "
         << (root->right->nextRight ? root->right->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->left->data << " is "
         << (root->left->left->nextRight ? root->left->left->nextRight->data : -1) << endl;
    return 0;
}
// this code is contributed by Kapil Poonia


Java
import java.util.*;
import java.io.*;
class Node {
    int data;
    Node left, right, nextRight;
 
    Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree {
    Node root;
    void connect(Node p)
    {
        // initialize queue to hold nodes at same level
        Queue q = new LinkedList<>();
 
        q.add(root); // adding nodes to the queue
 
        Node temp = null; // initializing prev to null
        while (!q.isEmpty()) {
            int n = q.size();
            for (int i = 0; i < n; i++) {
                Node prev = temp;
                temp = q.poll();
 
                // i > 0 because when i is 0 prev points
                // the last node of previous level,
                // so we skip it
                if (i > 0)
                    prev.nextRight = temp;
 
                if (temp.left != null)
                    q.add(temp.left);
 
                if (temp.right != null)
                    q.add(temp.right);
            }
 
            // pointing last node of the nth level to null
            temp.nextRight = null;
        }
    }
 
    // Driver program to test above functions
    public static void main(String args[])
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        System.out.println("Following are populated nextRight pointers in "
                           + "the tree"
                           + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.data + " is "
                           + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.left.data + " is "
                           + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.right.data + " is "
                           + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.left.left.data + " is "
                           + d);
    }
}
// This code has been contributed by Rahul Shakya


C#
// C# program to connect nodes
// at same level
using System;
using System.Collections.Generic;
 
class Node
{
    public int data;
    public Node left, right, nextRight;
 
    public Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree
{
    Node root;
    void connect(Node p)
    {
        // initialize queue to hold nodes at same level
        Queue q = new Queue();
 
        q.Enqueue(root); // adding nodes to tehe queue
 
        Node temp = null; // initializing prev to null
        while (q.Count > 0)
        {
            int n = q.Count;
            for (int i = 0; i < n; i++)
            {
                Node prev = temp;
                temp = q.Dequeue();
 
                // i > 0 because when i is 0 prev points
                // the last node of previous level,
                // so we skip it
                if (i > 0)
                    prev.nextRight = temp;
 
                if (temp.left != null)
                    q.Enqueue(temp.left);
 
                if (temp.right != null)
                    q.Enqueue(temp.right);
            }
 
            // pointing last node of the nth level to null
            temp.nextRight = null;
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
            10
            / \
        8     2
        /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        Console.WriteLine("Following are populated nextRight pointers in "
                        + "the tree"
                        + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.data + " is "
                        + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.data + " is "
                        + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.right.data + " is "
                        + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.left.data + " is "
                        + d);
 
        Console.ReadKey();
    }
}
 
// This code has been contributed by techno2mahi


Javascript


C++
// CPP program to connect nodes
// at same level using extended
// pre-order traversal
#include 
#include 
using namespace std;
 
class node {
public:
    int data;
    node* left;
    node* right;
    node* nextRight;
 
    /* Constructor that allocates a new node with the
    given data and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
        this->nextRight = NULL;
    }
};
 
void connectRecur(node* p);
 
// Sets the nextRight of
// root and calls connectRecur()
// for other nodes
void connect(node* p)
{
    // Set the nextRight for root
    p->nextRight = NULL;
 
    // Set the next right for rest of the nodes
    // (other than root)
    connectRecur(p);
}
 
/* Set next right of all descendants of p.
Assumption: p is a complete binary tree */
void connectRecur(node* p)
{
    // Base case
    if (!p)
        return;
 
    // Set the nextRight pointer for p's left child
    if (p->left)
        p->left->nextRight = p->right;
 
    // Set the nextRight pointer
    // for p's right child p->nextRight
    // will be NULL if p is the right
    // most child at its level
    if (p->right)
        p->right->nextRight = (p->nextRight) ? p->nextRight->left : NULL;
 
    // Set nextRight for other
    // nodes in pre order fashion
    connectRecur(p->left);
    connectRecur(p->right);
}
 
/* Driver code*/
int main()
{
 
    /* Constructed binary tree is
                10
            / \
            8 2
        /
        3
    */
    node* root = new node(10);
    root->left = new node(8);
    root->right = new node(2);
    root->left->left = new node(3);
 
    // Populates nextRight pointer in all nodes
    connect(root);
 
    // Let us check the values
    // of nextRight pointers
    cout << "Following are populated nextRight pointers in the tree"
            " (-1 is printed if there is no nextRight)\n";
    cout << "nextRight of " << root->data << " is "
         << (root->nextRight ? root->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->data << " is "
         << (root->left->nextRight ? root->left->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->right->data << " is "
         << (root->right->nextRight ? root->right->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->left->data << " is "
         << (root->left->left->nextRight ? root->left->left->nextRight->data : -1) << endl;
    return 0;
}
 
// This code is contributed by rathbhupendra


C
// C program to connect nodes at same level using extended
// pre-order traversal
#include 
#include 
 
struct node {
    int data;
    struct node* left;
    struct node* right;
    struct node* nextRight;
};
 
void connectRecur(struct node* p);
 
// Sets the nextRight of root and calls connectRecur()
// for other nodes
void connect(struct node* p)
{
    // Set the nextRight for root
    p->nextRight = NULL;
 
    // Set the next right for rest of the nodes
    // (other than root)
    connectRecur(p);
}
 
/* Set next right of all descendants of p.
   Assumption:  p is a complete binary tree */
void connectRecur(struct node* p)
{
    // Base case
    if (!p)
        return;
 
    // Set the nextRight pointer for p's left child
    if (p->left)
        p->left->nextRight = p->right;
 
    // Set the nextRight pointer for p's right child
    // p->nextRight will be NULL if p is the right
    // most child at its level
    if (p->right)
        p->right->nextRight = (p->nextRight) ? p->nextRight->left : NULL;
 
    // Set nextRight for other nodes in pre order fashion
    connectRecur(p->left);
    connectRecur(p->right);
}
 
/* UTILITY FUNCTIONS */
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct node* newnode(int data)
{
    struct node* node = (struct node*)
        malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    node->nextRight = NULL;
 
    return (node);
}
 
/* Driver program to test above functions*/
int main()
{
 
    /* Constructed binary tree is
            10
          /   \
        8      2
      /
    3
  */
    struct node* root = newnode(10);
    root->left = newnode(8);
    root->right = newnode(2);
    root->left->left = newnode(3);
 
    // Populates nextRight pointer in all nodes
    connect(root);
 
    // Let us check the values of nextRight pointers
    printf("Following are populated nextRight pointers in the tree "
           "(-1 is printed if there is no nextRight) \n");
    printf("nextRight of %d is %d \n", root->data,
           root->nextRight ? root->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->data,
           root->left->nextRight ? root->left->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->right->data,
           root->right->nextRight ? root->right->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->left->data,
           root->left->left->nextRight ? root->left->left->nextRight->data : -1);
    return 0;
}


Java
// JAVA program to connect nodes
// at same level using extended
// pre-order traversal
import java.util.*;
class GFG {
 
    static class node {
 
        int data;
        node left;
        node right;
        node nextRight;
 
        /*
         * Constructor that allocates a new node with the given data and null left and
         * right pointers.
         */
        node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
            this.nextRight = null;
        }
    };
 
    // Sets the nextRight of
    // root and calls connectRecur()
    // for other nodes
    static void connect(node p) {
        // Set the nextRight for root
        p.nextRight = null;
 
        // Set the next right for rest of the nodes
        // (other than root)
        connectRecur(p);
    }
 
    /*
     * Set next right of all descendants of p. Assumption: p is a complete binary
     * tree
     */
    static void connectRecur(node p) {
        // Base case
        if (p == null)
            return;
 
        // Set the nextRight pointer for p's left child
        if (p.left != null)
            p.left.nextRight = p.right;
 
        // Set the nextRight pointer
        // for p's right child p.nextRight
        // will be null if p is the right
        // most child at its level
        if (p.right != null)
            p.right.nextRight = (p.nextRight) != null ? p.nextRight.left : null;
 
        // Set nextRight for other
        // nodes in pre order fashion
        connectRecur(p.left);
        connectRecur(p.right);
    }
 
    /* Driver code */
    public static void main(String[] args) {
 
        /*
         * Constructed binary tree is 10 / \ 8 2 / 3
         */
        node root = new node(10);
        root.left = new node(8);
        root.right = new node(2);
        root.left.left = new node(3);
 
        // Populates nextRight pointer in all nodes
        connect(root);
 
        // Let us check the values
        // of nextRight pointers
        System.out.print("Following are populated nextRight pointers in the tree"
                + " (-1 is printed if there is no nextRight)\n");
        System.out.print(
                "nextRight of " + root.data + " is " + (root.nextRight != null ? root.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.left.data + " is "
                + (root.left.nextRight != null ? root.left.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.right.data + " is "
                + (root.right.nextRight != null ? root.right.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.left.left.data + " is "
                + (root.left.left.nextRight != null ? root.left.left.nextRight.data : -1) + "\n");
    }
}
 
// This code is contributed by umadevi9616


Python3
# Python3 program to connect nodes at same
# level using extended pre-order traversal
 
class newnode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = self.nextRight = None
         
# Sets the nextRight of root and calls
# connectRecur() for other nodes
def connect (p):
     
    # Set the nextRight for root
    p.nextRight = None
 
    # Set the next right for rest of
    # the nodes (other than root)
    connectRecur(p)
 
# Set next right of all descendants of p.
# Assumption: p is a complete binary tree
def connectRecur(p):
     
    # Base case
    if (not p):
        return
     
    # Set the nextRight pointer for p's
    # left child
    if (p.left):
        p.left.nextRight = p.right
     
    # Set the nextRight pointer for p's right
    # child p.nextRight will be None if p is
    # the right most child at its level
    if (p.right):
        if p.nextRight:
            p.right.nextRight = p.nextRight.left
        else:
            p.right.nextRight = None
     
    # Set nextRight for other nodes in
    # pre order fashion
    connectRecur(p.left)
    connectRecur(p.right)
 
# Driver Code
if __name__ == '__main__':
 
    # Constructed binary tree is
    # 10
    #     / \
    # 8     2
    # /
    # 3
    root = newnode(10)
    root.left = newnode(8)
    root.right = newnode(2)
    root.left.left = newnode(3)
 
    # Populates nextRight pointer in all nodes
    connect(root)
 
    # Let us check the values of nextRight pointers
    print("Following are populated nextRight",
          "pointers in the tree (-1 is printed",
                    "if there is no nextRight)")
    print("nextRight of", root.data, "is ", end = "")
    if root.nextRight:
        print(root.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.left.data, "is ", end = "")
    if root.left.nextRight:
        print(root.left.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.right.data, "is ", end = "")
    if root.right.nextRight:
        print(root.right.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.left.left.data, "is ", end = "")
    if root.left.left.nextRight:
        print(root.left.left.nextRight.data)
    else:
        print(-1)
 
# This code is contributed by PranchalK


C#
using System;
 
// C# program to connect nodes at same level using extended
// pre-order traversal
 
// A binary tree node
public class Node {
    public int data;
    public Node left, right, nextRight;
 
    public Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree {
    public Node root;
 
    // Sets the nextRight of root and calls connectRecur()
    // for other nodes
    public virtual void connect(Node p)
    {
 
        // Set the nextRight for root
        p.nextRight = null;
 
        // Set the next right for rest of the nodes (other
        // than root)
        connectRecur(p);
    }
 
    /* Set next right of all descendants of p.
       Assumption:  p is a complete binary tree */
    public virtual void connectRecur(Node p)
    {
        // Base case
        if (p == null) {
            return;
        }
 
        // Set the nextRight pointer for p's left child
        if (p.left != null) {
            p.left.nextRight = p.right;
        }
 
        // Set the nextRight pointer for p's right child
        // p->nextRight will be NULL if p is the right most child
        // at its level
        if (p.right != null) {
            p.right.nextRight = (p.nextRight != null) ? p.nextRight.left : null;
        }
 
        // Set nextRight for other nodes in pre order fashion
        connectRecur(p.left);
        connectRecur(p.right);
    }
 
    // Driver program to test above functions
    public static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        Console.WriteLine("Following are populated nextRight pointers in "
                          + "the tree"
                          + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.data + " is " + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.data + " is " + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.right.data + " is " + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.left.data + " is " + d);
    }
}
 
// This code is contributed by Shrikant13


最初,所有 nextRight 指针都指向垃圾值。您的函数应将这些指针设置为指向每个节点的右下角。
例子:

Input Tree
       A
      / \
     B   C
    / \   \
   D   E   F


Output Tree
       A--->NULL
      / \
     B-->C-->NULL
    / \   \
   D-->E-->F-->NULL

方法 1(扩展级顺序遍历或 BFS)
考虑Level Order Traversal的方法2。方法2可以很容易地扩展到连接同级节点。我们可以增加队列条目以包含节点级别,对于根节点也为 0,对于根节点的子节点为 1,依此类推。所以队列节点现在将包含一个指向树节点的指针和一个整数级别。当我们将一个节点加入队列时,我们确保在队列中设置了正确的节点级别值。设置nextRight,对于每个节点N,将下一个节点从队列中出队,如果下一个节点的层级相同,则将N的nextRight设置为出队节点的地址,否则将N的nextRight设置为NULL。
我们初始化一个指向前一个节点的节点 Prev。在遍历同一级别的节点时,我们会跟踪前一个节点,并在每次迭代中将 nextRight 指针指向当前节点。

C++

/* Iterative program to connect all the adjacent nodes at the same level in a binary tree*/
#include 
#include
using namespace std;
 
// A Binary Tree Node
class node {
public:
    int data;
    node* left;
    node* right;
    node* nextRight;
  
    /* Constructor that allocates a new node with the
    given data and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
        this->nextRight = NULL;
    }
};
// setting right pointer to next right node
/*
             10 ----------> NULL
            /  \
          8 --->2 --------> NULL
         /
        3 ----------------> NULL
        */
void connect(node *root)
    {
       //Base condition
       if(root==NULL)
        return;
       // Create an empty queue like level order traversal
       queue q;
       q.push(root);
       while(!q.empty()){
         // size indicates no. of nodes at current level
           int size=q.size();
         // for keeping track of previous node
           node* prev=NULL;
           while(size--){
               node* temp=q.front();
               q.pop();
              
               if(temp->left)
                q.push(temp->left);
              
               if(temp->right)
                q.push(temp->right);
               
               if(prev!=NULL)
                prev->nextRight=temp;
               prev=temp;
           }
           prev->nextRight=NULL;
       }
    }
 
int main() {
      /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
    // Let us create binary tree shown above
    node* root = new node(10);
    root->left = new node(8);
    root->right = new node(2);
    root->left->left = new node(3);
    connect(root);
    // Let us check the values
    // of nextRight pointers
    cout << "Following are populated nextRight pointers in the tree"
            " (-1 is printed if there is no nextRight)\n";
    cout << "nextRight of " << root->data << " is "
         << (root->nextRight ? root->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->data << " is "
         << (root->left->nextRight ? root->left->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->right->data << " is "
         << (root->right->nextRight ? root->right->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->left->data << " is "
         << (root->left->left->nextRight ? root->left->left->nextRight->data : -1) << endl;
    return 0;
}
// this code is contributed by Kapil Poonia

Java

import java.util.*;
import java.io.*;
class Node {
    int data;
    Node left, right, nextRight;
 
    Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree {
    Node root;
    void connect(Node p)
    {
        // initialize queue to hold nodes at same level
        Queue q = new LinkedList<>();
 
        q.add(root); // adding nodes to the queue
 
        Node temp = null; // initializing prev to null
        while (!q.isEmpty()) {
            int n = q.size();
            for (int i = 0; i < n; i++) {
                Node prev = temp;
                temp = q.poll();
 
                // i > 0 because when i is 0 prev points
                // the last node of previous level,
                // so we skip it
                if (i > 0)
                    prev.nextRight = temp;
 
                if (temp.left != null)
                    q.add(temp.left);
 
                if (temp.right != null)
                    q.add(temp.right);
            }
 
            // pointing last node of the nth level to null
            temp.nextRight = null;
        }
    }
 
    // Driver program to test above functions
    public static void main(String args[])
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        System.out.println("Following are populated nextRight pointers in "
                           + "the tree"
                           + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.data + " is "
                           + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.left.data + " is "
                           + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.right.data + " is "
                           + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        System.out.println("nextRight of " + tree.root.left.left.data + " is "
                           + d);
    }
}
// This code has been contributed by Rahul Shakya

C#

// C# program to connect nodes
// at same level
using System;
using System.Collections.Generic;
 
class Node
{
    public int data;
    public Node left, right, nextRight;
 
    public Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree
{
    Node root;
    void connect(Node p)
    {
        // initialize queue to hold nodes at same level
        Queue q = new Queue();
 
        q.Enqueue(root); // adding nodes to tehe queue
 
        Node temp = null; // initializing prev to null
        while (q.Count > 0)
        {
            int n = q.Count;
            for (int i = 0; i < n; i++)
            {
                Node prev = temp;
                temp = q.Dequeue();
 
                // i > 0 because when i is 0 prev points
                // the last node of previous level,
                // so we skip it
                if (i > 0)
                    prev.nextRight = temp;
 
                if (temp.left != null)
                    q.Enqueue(temp.left);
 
                if (temp.right != null)
                    q.Enqueue(temp.right);
            }
 
            // pointing last node of the nth level to null
            temp.nextRight = null;
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
            10
            / \
        8     2
        /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        Console.WriteLine("Following are populated nextRight pointers in "
                        + "the tree"
                        + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.data + " is "
                        + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.data + " is "
                        + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.right.data + " is "
                        + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.left.data + " is "
                        + d);
 
        Console.ReadKey();
    }
}
 
// This code has been contributed by techno2mahi

Javascript


输出:
Following are populated nextRight pointers in the tree(-1 is printed if there is no nextRight)
nextRight of 10 is -1
nextRight of 8 is 2
nextRight of 2 is -1
nextRight of 3 is -1

请参考连接同层节点(Level Order Traversal)实现。
时间复杂度:O(n)
方法二(扩展前序遍历)
此方法仅适用于完全二叉树。在此方法中,我们以 Pre Order 方式设置 nextRight 以确保父级的 nextRight 设置在其子级之前。当我们在节点 p 处时,我们设置它的左右子节点的 nextRight。由于树是完全树,p 的左孩子 (p->left->nextRight) 的 nextRight 将始终是 p 的右孩子,p 的右孩子 (p->right->nextRight) 的 nextRight 将始终是 p 的左孩子nextRight(如果 p 不是其级别的最右边节点)。如果 p 是最右边的节点,那么 p 的右孩子的 nextRight 将为 NULL。

C++

// CPP program to connect nodes
// at same level using extended
// pre-order traversal
#include 
#include 
using namespace std;
 
class node {
public:
    int data;
    node* left;
    node* right;
    node* nextRight;
 
    /* Constructor that allocates a new node with the
    given data and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
        this->nextRight = NULL;
    }
};
 
void connectRecur(node* p);
 
// Sets the nextRight of
// root and calls connectRecur()
// for other nodes
void connect(node* p)
{
    // Set the nextRight for root
    p->nextRight = NULL;
 
    // Set the next right for rest of the nodes
    // (other than root)
    connectRecur(p);
}
 
/* Set next right of all descendants of p.
Assumption: p is a complete binary tree */
void connectRecur(node* p)
{
    // Base case
    if (!p)
        return;
 
    // Set the nextRight pointer for p's left child
    if (p->left)
        p->left->nextRight = p->right;
 
    // Set the nextRight pointer
    // for p's right child p->nextRight
    // will be NULL if p is the right
    // most child at its level
    if (p->right)
        p->right->nextRight = (p->nextRight) ? p->nextRight->left : NULL;
 
    // Set nextRight for other
    // nodes in pre order fashion
    connectRecur(p->left);
    connectRecur(p->right);
}
 
/* Driver code*/
int main()
{
 
    /* Constructed binary tree is
                10
            / \
            8 2
        /
        3
    */
    node* root = new node(10);
    root->left = new node(8);
    root->right = new node(2);
    root->left->left = new node(3);
 
    // Populates nextRight pointer in all nodes
    connect(root);
 
    // Let us check the values
    // of nextRight pointers
    cout << "Following are populated nextRight pointers in the tree"
            " (-1 is printed if there is no nextRight)\n";
    cout << "nextRight of " << root->data << " is "
         << (root->nextRight ? root->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->data << " is "
         << (root->left->nextRight ? root->left->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->right->data << " is "
         << (root->right->nextRight ? root->right->nextRight->data : -1) << endl;
    cout << "nextRight of " << root->left->left->data << " is "
         << (root->left->left->nextRight ? root->left->left->nextRight->data : -1) << endl;
    return 0;
}
 
// This code is contributed by rathbhupendra

C

// C program to connect nodes at same level using extended
// pre-order traversal
#include 
#include 
 
struct node {
    int data;
    struct node* left;
    struct node* right;
    struct node* nextRight;
};
 
void connectRecur(struct node* p);
 
// Sets the nextRight of root and calls connectRecur()
// for other nodes
void connect(struct node* p)
{
    // Set the nextRight for root
    p->nextRight = NULL;
 
    // Set the next right for rest of the nodes
    // (other than root)
    connectRecur(p);
}
 
/* Set next right of all descendants of p.
   Assumption:  p is a complete binary tree */
void connectRecur(struct node* p)
{
    // Base case
    if (!p)
        return;
 
    // Set the nextRight pointer for p's left child
    if (p->left)
        p->left->nextRight = p->right;
 
    // Set the nextRight pointer for p's right child
    // p->nextRight will be NULL if p is the right
    // most child at its level
    if (p->right)
        p->right->nextRight = (p->nextRight) ? p->nextRight->left : NULL;
 
    // Set nextRight for other nodes in pre order fashion
    connectRecur(p->left);
    connectRecur(p->right);
}
 
/* UTILITY FUNCTIONS */
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct node* newnode(int data)
{
    struct node* node = (struct node*)
        malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    node->nextRight = NULL;
 
    return (node);
}
 
/* Driver program to test above functions*/
int main()
{
 
    /* Constructed binary tree is
            10
          /   \
        8      2
      /
    3
  */
    struct node* root = newnode(10);
    root->left = newnode(8);
    root->right = newnode(2);
    root->left->left = newnode(3);
 
    // Populates nextRight pointer in all nodes
    connect(root);
 
    // Let us check the values of nextRight pointers
    printf("Following are populated nextRight pointers in the tree "
           "(-1 is printed if there is no nextRight) \n");
    printf("nextRight of %d is %d \n", root->data,
           root->nextRight ? root->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->data,
           root->left->nextRight ? root->left->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->right->data,
           root->right->nextRight ? root->right->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->left->data,
           root->left->left->nextRight ? root->left->left->nextRight->data : -1);
    return 0;
}

Java

// JAVA program to connect nodes
// at same level using extended
// pre-order traversal
import java.util.*;
class GFG {
 
    static class node {
 
        int data;
        node left;
        node right;
        node nextRight;
 
        /*
         * Constructor that allocates a new node with the given data and null left and
         * right pointers.
         */
        node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
            this.nextRight = null;
        }
    };
 
    // Sets the nextRight of
    // root and calls connectRecur()
    // for other nodes
    static void connect(node p) {
        // Set the nextRight for root
        p.nextRight = null;
 
        // Set the next right for rest of the nodes
        // (other than root)
        connectRecur(p);
    }
 
    /*
     * Set next right of all descendants of p. Assumption: p is a complete binary
     * tree
     */
    static void connectRecur(node p) {
        // Base case
        if (p == null)
            return;
 
        // Set the nextRight pointer for p's left child
        if (p.left != null)
            p.left.nextRight = p.right;
 
        // Set the nextRight pointer
        // for p's right child p.nextRight
        // will be null if p is the right
        // most child at its level
        if (p.right != null)
            p.right.nextRight = (p.nextRight) != null ? p.nextRight.left : null;
 
        // Set nextRight for other
        // nodes in pre order fashion
        connectRecur(p.left);
        connectRecur(p.right);
    }
 
    /* Driver code */
    public static void main(String[] args) {
 
        /*
         * Constructed binary tree is 10 / \ 8 2 / 3
         */
        node root = new node(10);
        root.left = new node(8);
        root.right = new node(2);
        root.left.left = new node(3);
 
        // Populates nextRight pointer in all nodes
        connect(root);
 
        // Let us check the values
        // of nextRight pointers
        System.out.print("Following are populated nextRight pointers in the tree"
                + " (-1 is printed if there is no nextRight)\n");
        System.out.print(
                "nextRight of " + root.data + " is " + (root.nextRight != null ? root.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.left.data + " is "
                + (root.left.nextRight != null ? root.left.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.right.data + " is "
                + (root.right.nextRight != null ? root.right.nextRight.data : -1) + "\n");
        System.out.print("nextRight of " + root.left.left.data + " is "
                + (root.left.left.nextRight != null ? root.left.left.nextRight.data : -1) + "\n");
    }
}
 
// This code is contributed by umadevi9616

Python3

# Python3 program to connect nodes at same
# level using extended pre-order traversal
 
class newnode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = self.nextRight = None
         
# Sets the nextRight of root and calls
# connectRecur() for other nodes
def connect (p):
     
    # Set the nextRight for root
    p.nextRight = None
 
    # Set the next right for rest of
    # the nodes (other than root)
    connectRecur(p)
 
# Set next right of all descendants of p.
# Assumption: p is a complete binary tree
def connectRecur(p):
     
    # Base case
    if (not p):
        return
     
    # Set the nextRight pointer for p's
    # left child
    if (p.left):
        p.left.nextRight = p.right
     
    # Set the nextRight pointer for p's right
    # child p.nextRight will be None if p is
    # the right most child at its level
    if (p.right):
        if p.nextRight:
            p.right.nextRight = p.nextRight.left
        else:
            p.right.nextRight = None
     
    # Set nextRight for other nodes in
    # pre order fashion
    connectRecur(p.left)
    connectRecur(p.right)
 
# Driver Code
if __name__ == '__main__':
 
    # Constructed binary tree is
    # 10
    #     / \
    # 8     2
    # /
    # 3
    root = newnode(10)
    root.left = newnode(8)
    root.right = newnode(2)
    root.left.left = newnode(3)
 
    # Populates nextRight pointer in all nodes
    connect(root)
 
    # Let us check the values of nextRight pointers
    print("Following are populated nextRight",
          "pointers in the tree (-1 is printed",
                    "if there is no nextRight)")
    print("nextRight of", root.data, "is ", end = "")
    if root.nextRight:
        print(root.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.left.data, "is ", end = "")
    if root.left.nextRight:
        print(root.left.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.right.data, "is ", end = "")
    if root.right.nextRight:
        print(root.right.nextRight.data)
    else:
        print(-1)
    print("nextRight of", root.left.left.data, "is ", end = "")
    if root.left.left.nextRight:
        print(root.left.left.nextRight.data)
    else:
        print(-1)
 
# This code is contributed by PranchalK

C#

using System;
 
// C# program to connect nodes at same level using extended
// pre-order traversal
 
// A binary tree node
public class Node {
    public int data;
    public Node left, right, nextRight;
 
    public Node(int item)
    {
        data = item;
        left = right = nextRight = null;
    }
}
 
public class BinaryTree {
    public Node root;
 
    // Sets the nextRight of root and calls connectRecur()
    // for other nodes
    public virtual void connect(Node p)
    {
 
        // Set the nextRight for root
        p.nextRight = null;
 
        // Set the next right for rest of the nodes (other
        // than root)
        connectRecur(p);
    }
 
    /* Set next right of all descendants of p.
       Assumption:  p is a complete binary tree */
    public virtual void connectRecur(Node p)
    {
        // Base case
        if (p == null) {
            return;
        }
 
        // Set the nextRight pointer for p's left child
        if (p.left != null) {
            p.left.nextRight = p.right;
        }
 
        // Set the nextRight pointer for p's right child
        // p->nextRight will be NULL if p is the right most child
        // at its level
        if (p.right != null) {
            p.right.nextRight = (p.nextRight != null) ? p.nextRight.left : null;
        }
 
        // Set nextRight for other nodes in pre order fashion
        connectRecur(p.left);
        connectRecur(p.right);
    }
 
    // Driver program to test above functions
    public static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
 
        /* Constructed binary tree is
             10
            /  \
          8     2
         /
        3
        */
        tree.root = new Node(10);
        tree.root.left = new Node(8);
        tree.root.right = new Node(2);
        tree.root.left.left = new Node(3);
 
        // Populates nextRight pointer in all nodes
        tree.connect(tree.root);
 
        // Let us check the values of nextRight pointers
        Console.WriteLine("Following are populated nextRight pointers in "
                          + "the tree"
                          + "(-1 is printed if there is no nextRight)");
        int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.data + " is " + a);
        int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.data + " is " + b);
        int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.right.data + " is " + c);
        int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
        Console.WriteLine("nextRight of " + tree.root.left.left.data + " is " + d);
    }
}
 
// This code is contributed by Shrikant13
输出:
Following are populated nextRight pointers in the tree (-1 is printed if there is no nextRight)
nextRight of 10 is -1
nextRight of 8 is 2
nextRight of 2 is -1
nextRight of 3 is -1

感谢 Dhanya 提出这种方法。
时间复杂度: O(n)
为什么方法 2 不适用于不是完全二叉树的树?
让我们以下面的树为例。在方法 2 中,我们以预排序方式设置 nextRight 指针。当我们在节点 4 时,我们设置其子节点的 nextRight 是 8 和 9(4 的 nextRight 已经设置为节点 5)。 8 的 nextRight 将简单地设置为 9,但 9 的 nextRight 将设置为 NULL,这是不正确的。我们无法设置正确的nextRight,因为当我们设置nextRight 为9 时,我们只有节点4 的nextRight 和节点4 的祖先,我们在根的右子树中没有节点的nextRight。

1
          /    \
        2        3
       / \      /  \
      4   5    6    7
     / \           / \  
    8   9        10   11