leetcode【116-117】Populating Next Right Pointers in Each Node I / II【c++,递归与非递归】

问题描述(116):

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

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: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect 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.

源码(116):

和第102的层次遍历leetcode【102,103】Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal有点像。这思路比较简单的方法,当然效率不高了【笑脸】,时间42%,空间100%。唯一用到满二叉树概念就是每一层的节点数。

/*
// 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)    return root;
        queue<Node*> st;
        Node* cur = root;
        int level = 1, last_level = level;
        st.push(cur);
        while(!st.empty()){
            Node* tmp = st.front();
            // cout<<tmp->val<<" ";
            st.pop();
            if(tmp->left)       st.push(tmp->left);
            if(tmp->right)      st.push(tmp->right);
            level--;
            if(level == 0){
                tmp->next = NULL;
                level = last_level*2;
                last_level = level;
                continue;
            }
            tmp->next = st.front();
        }
        return root;
    }
};

DISCUSS区有人用递归的方式,效率稍微高点,可能是运气原因,大家都是遍历了一遍。时间71%,空间100%。

class Solution {
public:
    void connect_help(Node *root, Node *parent){
        if(!parent)     root->next = NULL;
        else if(parent->left == root)   root->next = parent->right;
        else if(parent->right == root)     root->next = parent->next? parent->next->left: NULL;
        if(root->left){
            connect_help(root->left, root);
            connect_help(root->right, root);
        }
    }
    Node* connect(Node *root) {
        if(!root)   return root;
        connect_help(root, NULL);
        return root;
    }
};

问题描述(117):

 

源码(117):

和上一题差不多,就是用一个变量保存下一层节点的个数。效率还可以,时间78%,空间100%。

/*
// 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)    return root;
        queue<Node*> st;
        Node* cur = root;
        int level = 1, nex = 0;
        st.push(cur);
        while(!st.empty()){
            Node* tmp = st.front();
            // cout<<tmp->val<<" ";
            st.pop();
            if(tmp->left){
                st.push(tmp->left);     nex++;
            }
            if(tmp->right){
                st.push(tmp->right);    nex++;
            }
            level--;
            if(level == 0){
                tmp->next = NULL;
                level = nex;
                nex = 0;
                continue;
            }
            tmp->next = st.front();
        }
        return root;
    }
};

网上一些做法:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    Node* connect(Node* root) {
        Node* result = root;
        while(root){
            Node *dump = new Node(0);
            auto cur = dump;
            while(root){
                if(root->left){
                    cur->next = root->left;
                    cur = cur->next;
                }
                if(root->right){
                    cur->next = root->right;
                    cur = cur->next;
                }
                root = root->next;
            }
            root = dump->next;
        }
        return result;
    }
};

网上的递归方案:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void nextNode(Node* root){
        if(root->next){
            Node* Next = root->next;
            while(!Next->left && !Next->right){
                if(Next->next)      Next = Next->next;
                else    break;
            }
            if(root->right && Next->left)   root->right->next = Next->left;
            else if(root->right && Next->right)     root->right->next = Next->right;
            else if(root->left && Next->left)       root->left->next = Next->left;
            else if(root->left && Next->right)      root->left->next = Next->right;
        }
    }
    Node* help(Node* root){
        if(!root->left && !root->right)     return root;
        if(root->left && root->right)    root->left->next = root->right;
        nextNode(root);
        if(root->right)     help(root->right);
        if(root->left)      help(root->left);
        return root;
    }
    Node* connect(Node* root) {
        if(!root)   return root;
        return help(root);
        // return root;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值