给每一个结点添加向右的next指针结点


版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


  • 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 next pointer should be set to NULL.

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指针结点,指针指向右边的下一个结点。如果没有下一个右边结点,next指针应该被设置为指向NULL

    提示:(1)只能使用固定的额外空间;(2)假设二叉树都是完美的二叉树(即,所有的叶子都在同一层上,而且每个父结点都有两个孩子结点)。

    就像例子中给出的就是完美二叉树。

    在调用完成的函数后,二叉树的结构变成了上面那样。

  • 思路:仔细考虑题目要求,发现是在同一层上进行操作,应该想到用层次法对二叉树进行操作,思路如下,判断当前节点如果不是叶子结点,说明他有两个孩子结点,将左孩子结点指向右孩子结点,再判断该结点有没有next结点,如果有,那他也有两个孩子结点,就可以把当前结点的右孩子结点指向next结点的左孩子结点,然后再判断next结点的情况,判断完这一层的,再去判断下一层。

    当然还有递归的思想就比较简单了。

  • 代码:(递归)

// 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 connect(TreeLinkNode *root) {
        if(root == NULL) return; //当前节点存在
        // 当前结点不是叶子结点,那必然就有两个结点,就让左孩子结点的next指向右孩子结点
        if(root->left!=NULL&&root->right!=NULL)
            root->left->next=root->right;
        // 当前结点不是叶子结点,且有next结点
        if(root->right!=NULL&&root->next!=NULL)
            root->right->next=root->next->left;
        connect(root->left); // 递归判断左子树的情况
        connect(root->right); // 递归判断右子树的情况
    }
};

(非递归)

// 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 connect(TreeLinkNode *root) {
        TreeLinkNode *r = root;
        while(r && r->left){ // 结点非空,且存在孩子结点(左右结点判断一个就好,一个有就都有)
            TreeLinkNode *cur = r;
            while(cur && cur->left){ // 结点非空,且存在孩子结点(左右结点判断一个就好,一个有就都有)
                // 让当前结点的左孩子结点的next指向当前结点的右孩子结点
                cur->left->next = cur->right;
                // 如果当前结点有next结点,
                // 就让当前结点的右孩子结点的next指向当前结点的next结点的左孩子结点
                // (当前结点有孩子结点,说明当前节点的next结点也必有孩子结点)
                cur->right->next = cur->next == NULL ? NULL : cur->next->left;
                // 层序向右
                cur = cur->next;
            }//while
            // 层序向下
            r = r->left;
        }//while
    }
};
  • 以上。

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值