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

来源:互联网 发布:java方法的概念 编辑:程序博客网 时间:2024/06/06 02:40

题干

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
原创粉丝点击