【LeetCode】populating-next-roght-pointers-in-each-node i&ii

题干

populating-next-roght-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 next pointer should be set toNULL.
Initially, all next pointers are set toNULL.
Note:
You may only use constant extra space.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

After calling your function, the tree should look like:

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

把树结点的next指针指向下一个同层下一个结点,如果同层没有,则为NULL,如例子所示。
要求:只能开辟常数量级的空间。假设为完全二叉树。

populating-next-roght-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) {}
 * };
 */

解题思路

问题一

做树的层次遍历,由于是完全二叉树,对于next赋值只有两种情况。

问题二

树的层次遍历,由于是非完全二叉树,需要用队列进行维护,记录每层的结点树,对当前结点next赋值的同时,如果存在left和right,压如队列中。直到队列中没有元素结束。

参考代码

问题一:
class Solution {
    public:
        void connect(TreeLinkNode *root) 
        {
            if (root==NULL)
                return;
            if (root->left!=NULL&&root->right!=NULL)
                root->left->next=root->right;//结点存在左子树一定存在右子树,且左结点next为右结点
            if (root->next!=NULL&&root->right!=NULL)//结点存在右子树,且next为同层结点,右子树next为next的左子树
                root->right->next=root->next->left;
            connect(root->left);
            connect(root->right);
        }
};
问题二:
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (root==NULL)
            return;
        queue<TreeLinkNode *>data;//维护队列
        TreeLinkNode *cur;

        data.push(root);

        while(!data.empty())
        {
            int len=data.size();//记录同层结点数
            for(int i=1;i<=len;i++)
            {
                cur=data.front();
                if (cur->left!=NULL)
                    data.push(cur->left);//压入左结点
                if (cur->right!=NULL)
                    data.push(cur->right);//压入右结点
                data.pop();
                if (i!=len)
                    cur->next=data.front();//当前结点next赋值
                else
                    cur->next=NULL;
            }
        }

    }
};

方法讨论

树的层序遍历变种。

易错点

计数

要记录每层结点的结点数,使得每层最后一个next指向NULL。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值