LeetCode happy brush title fifty-five days --117. Populating Next Right Pointers in Each Node II

Problems to be solved:

1. A problem has been bothering me, people want to see next is how to process the input tree, it is best to traverse this hierarchy is clear and intuitive way.

2. With regard to the use of pointers *

Therefore, lead to code is not complete, there is no main function calls the class of Solution

117. Populating Next Right Pointers in Each Node II
Medium

Given a binary tree

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

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

 

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

 

Example 1:

INPUT: the root = [1,2,3,4,5, null,. 7] 
the Output: [. 1, #, 2,3, #, 4,5,7, #] 
Explanation: The above the Given binary Tree (Figure A ), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level. 

this question is deformation on a question of difference:
on a complete binary tree without distinguishing whether there is a specific node of the tree, the tree will have to traverse recursive and non-recursive two kinds. Recursive method will be different, but non-recursive method
can also be applied. Non-recursive method is a good thing, and the combined stack, there will be an element onto the stack, it ignored the impact of the tree itself brings. This method is in space and
performed very well on the complexity, although there are double circulation, but the number of cycles performed is estimated to be less reason
#include<bits/stdc++.h>
/*
multi-line comment
*/
using namespace std;
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;
//three different kinds initialize
    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) {}
};
//definition of solution
class Solution{
public:
    Node* connect(Node* root){
        //special case deal
        if(!root) return NULL;
        //define queue store list by line
        queue<Node*> q;
        q.push(root);
        //still have node not iterating
        while(!q.empty())
        {
            int len=q.size();
            for(int i=0;i<len;++i)
            {
                Node *t=q.front();
                q.pop();
                if(i<len-1) t->next=q.front();
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
        }
        return root;
    }
};
 
  

 

 

Guess you like

Origin www.cnblogs.com/Marigolci/p/12008181.html