[email protected] [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
       /        2    3
     / \        4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \        4-> 5 -> 7 -> NULL
/**
 * 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 node {
    public:
        TreeLinkNode* nd;
        int lv;
        node(TreeLinkNode* rhs, int l): nd(rhs), lv(l) {}
};

class Solution {
public:
    void connect(TreeLinkNode *root) {
        if(!root)  return;

        queue<node> q;
        q.push(node(root, 0));
        vector<node> vec;

        while(!q.empty()) {
            node top = q.front();
            vec.push_back(top);
            q.pop();

            int cur_lv = top.lv;
            TreeLinkNode* cur_nd = top.nd;

            if(cur_nd->left)  q.push(node(cur_nd->left, cur_lv+1));
            if(cur_nd->right)  q.push(node(cur_nd->right, cur_lv+1));
        }

        int l = 0, r = 1;
        while(r < vec.size()) {
            vec[l].nd->next = NULL;
            while(r < vec.size() && vec[r].lv == vec[l].lv) {
                vec[l].nd->next = vec[r].nd;
                ++l;
                ++r;
            }

            l = r;
            r = l+1;
        }
        vec[vec.size()-1].nd->next = NULL;
    }
};
时间: 2024-08-05 20:37:29

[email protected] [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)的相关文章

【57】117. Populating Next Right Pointers in Each Node II

117. Populating Next Right Pointers in Each Node II Description Submission Solutions Add to List Total Accepted: 84339 Total Submissions: 251330 Difficulty: Medium Contributors: Admin Follow up for problem "Populating Next Right Pointers in Each Node

LeetCode开心刷题五十五天——117. Populating Next Right Pointers in Each Node II

问题亟待解决: 1.一个问题一直困扰着我,想看下别人是怎么处理树的输入的,最好是以层级遍历这种清楚直观的方式. 2.关于指针*的使用 因此也导致代码不完整,没有主函数对Solution类的调用 117. Populating Next Right Pointers in Each Node II Medium 1161165FavoriteShare Given a binary tree struct Node { int val; Node *left; Node *right; Node

leetcode -day14 Populating Next Right Pointers in Each Node I II

1.  Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the

Leetcode | Populating Next Right Pointers in Each Node I &amp; II

Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next

Java for LeetCode 117 Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

leetcode 117 Populating Next Right Pointers in Each Node II ----- java

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

117. Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

LeetCode 117:Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

[leedcode 117] Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr