Populating Next Right Pointers in Each Node

简介: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }  ...

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 to NULL.

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。

 

C++实现代码如下:

#include<iostream>
#include<new>
#include<queue>
using namespace std;


// 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;
        queue<TreeLinkNode*> q;
        q.push(root);
        TreeLinkNode *tmp=root;
        while(!q.empty())
        {
            tmp->next=q.front();
            tmp=q.front();
            q.pop();
            if(tmp->left)
                q.push(tmp->left);
            if(tmp->right)
                q.push(tmp->right);
        }
        tmp->next=NULL;
        while(root)
        {
            root->next=NULL;
            root=root->right;
        }
    }
    void createTree(TreeLinkNode *&root)
    {
        int i;
        cin>>i;
        if(i!=0)
        {
            root=new TreeLinkNode(i);
            if(root==NULL)
                return;
            createTree(root->left);
            createTree(root->right);
        }
    }
};

int main()
{
    Solution s;
    TreeLinkNode *root;
    s.createTree(root);
    s.connect(root);
    cout<<root->val<<endl;
    while(root->left)
    {
        cout<<root->left->val<<" ";
        TreeLinkNode *tmp=root->left->next;
        while(tmp)
        {
            cout<<tmp->val<<" ";
            tmp=tmp->next;
        }
        cout<<endl;
        root=root->left;
    }
}

运行结果:

 
相关文章
[LeetCode] Populating Next Right Pointers in Each Node
The idea is similar to a level-order traversal and remember to take full advantages of the prefect binary tree assumption in the problem statement.
899 0
[LeetCode] Populating Next Right Pointers in Each Node II
The problem becomes more difficult once the binary tree is not perfect. The idea is still similar to use a level-order traversal.
930 0
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 s...
917 0
|
10月前
|
JavaScript Unix Linux
nvm与node.js的安装指南
通过以上步骤,你可以在各种操作系统上成功安装NVM和Node.js,从而在不同的项目中灵活切换Node.js版本。这种灵活性对于管理不同项目的环境依赖而言是非常重要的。
3380 11
|
存储 JavaScript 搜索推荐
Node框架的安装和配置方法
安装 Node 框架是进行 Node 开发的第一步,通过正确的安装和配置,可以为后续的开发工作提供良好的基础。在安装过程中,需要仔细阅读相关文档和提示,遇到问题及时解决,以确保安装顺利完成。
1238 155
|
弹性计算 JavaScript 前端开发
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
Node.js 是一种高效的 JavaScript 运行环境,基于 Chrome V8 引擎,支持在服务器端运行 JavaScript 代码。本文介绍如何在阿里云上一键部署 Node.js 环境,无需繁琐配置,轻松上手。前提条件包括 ECS 实例运行中且操作系统为 CentOS、Ubuntu 等。功能特点为一键安装和稳定性好,支持常用 LTS 版本。安装步骤简单:登录阿里云控制台,选择扩展程序管理页面,安装 Node.js 扩展,选择实例和版本,等待创建完成并验证安装成功。通过阿里云的公共扩展,初学者和经验丰富的开发者都能快速进入开发状态,开启高效开发之旅。