[C + +] leetcode:123 populating Next right pointers in each Node (link full binary tree)

Source: Internet
Author: User

Topic:

Given a binary tree

    struct Treelinknode {      treelinknode *left;      Treelinknode *right;      Treelinknode *next;    }

Populate each of the next pointer to the next right node. If There is no next right node, the next pointer should are set to NULL .

Initially, all next pointers is set to NULL .

Note:

    • Constant extra space.
    • You could assume that it was a perfect binary tree (ie, all leaves was at the same level, and every parent had both children).

For example,
Given the following perfect binary tree,

         1       /        2    3     /\  /     4  5  6  7

After calling your function, the tree is should look like:

         1, NULL       /        2, 3, null     /\  /     4->5->6->7, NULL

Answer 1: Using queues

idea: We maintain a pre-order node and queue. The queue is used to press the traversed node into the queue, each time a node is fetched from the queue, if the pre-order node is not empty, we link the pre-order node and the current node, and update the pre-order node as the current node. Determines whether the current node is null, and if so, at the end of this layer, we use NULL to differentiate each layer. Note Only when the queue is non-empty, we join the layer's distinguished flag null, otherwise the last layer of the tree will cause a dead loop.

This approach although AC uses a queue to store nodes, the space complexity is O (LG (N)). is not optimal, the next method introduces an O (1) method

Attention:

1. How to connect the pre-order node and the current node.

if (pre! = NULL)            {                pre->next = node;            }            Pre = node;
AC Code:
/** * Definition for binary tree with next pointer. * struct Treelinknode {* int val; * Treelinknode *left, *right, *next; * Treelinknode (int x): Val (x), left (NULL), rig HT (NULL), Next (null) {}};        */class Solution {public:void Connect (Treelinknode *root) {queue<treelinknode*> Treeq;        if (root = NULL) return;        Treeq.push (root);        Treeq.push (NULL);                treelinknode* Pre (NULL);            while (!treeq.empty ()) {treelinknode* node = Treeq.front ();                        Treeq.pop ();            if (pre! = NULL) {pre->next = node;                        } Pre = node;                if (node! = NULL) {if (node->left) Treeq.push (node->left);            if (node->right) Treeq.push (node->right);     } else {if (!treeq.empty ()) {Treeq.push (NULL);     Differentiate each layer}       }} return; }};

Answer 2: Constant space complexity

idea: This is a question that requires us to maintain each layer of a tree as a linked list, a tree that is given a complete binary tree. Here we do not actually need to maintain a queue, because each layer here we will maintain a list of links from the list to get the next node information. The starting point for each level of the list is the leftmost node in each layer of the tree, i.e. we only need to maintain a list of starting pointers to get the entire queue in turn. We maintain two node pointers, one is the list start node for each layer of the queue, and one is the current root node, which is used to connect its children. If the current node is not empty, there is also a child node that needs to be connected. We connect the children to the point according to the topic request. After the first layer is over, we update the pre to the starting node of the next layer, i.e. Pre->left

Complexity: Time complexity, each node is traversed once, O (N), space does not need a queue, O (1)

AC Code:

/** * Definition for binary tree with next pointer. * struct Treelinknode {*  int val; *  Treelinknode *left, *right, *next; *  treelinknode (int x): Val (x), left (N ULL), right (null), Next (null) {} *}; */class Solution {public:    void Connect (Treelinknode *root) {        if (root = NULL) return;        treelinknode* pre = root;  The pre maintains the root node of the list starting with each row        treelinknode* cur = NULL;                while (Pre->left)        {            cur = pre;  Cur represents the root node of the two nodes currently being linked while            (cur)            {                Cur->left->next = cur->right;                if (cur->next) Cur->right->next = cur->next->left;                  Cur = cur->next; Cur jumps to the next root node of the peer            }            pre = pre->left;        }}    ;



[C + +] leetcode:123 populating Next right pointers in each Node (link full binary tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.