博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ - Populating Next Right Pointers in Each Node II
阅读量:4307 次
发布时间:2019-06-06

本文共 3661 字,大约阅读时间需要 12 分钟。

题目:

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

解题思路:

  方法一:直接进行广度优先遍历,在遍历的过程中对next指针赋值。

  方法二:可以利用生成的next指针来横向扫描,即得到一层的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;        }        queue
one; queue
another; one.push(root); TreeLinkNode* cur; TreeLinkNode* next; while(!(one.empty() && another.empty())) { if (!one.empty()) { cur = one.front(); one.pop(); if (cur->left != NULL) another.push(cur->left); if (cur->right != NULL) another.push(cur->right); while (!one.empty()) { next = one.front(); one.pop(); if (next->left != NULL) another.push(next->left); if (next->right != NULL) another.push(next->right); cur->next = next; cur = next; } cur->next = NULL; } if (!another.empty()) { cur = another.front(); another.pop(); if (cur->left != NULL) one.push(cur->left); if (cur->right != NULL) one.push(cur->right); while (!another.empty()) { next = another.front(); another.pop(); if (next->left != NULL) one.push(next->left); if (next->right != NULL) one.push(next->right); cur->next = next; cur = next; } cur->next = 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 Solution {public:    TreeLinkNode *findNext(TreeLinkNode *head)    {        while(head != NULL && head->left == NULL && head->right == NULL)            head = head->next;        return head;    }    void connect(TreeLinkNode *root) {        if(root == NULL) return;        TreeLinkNode *head, *last, *nexhead;        for(head = root; head != NULL; head = nexhead)        {            head = findNext(head);            if(head == NULL) break;            if(head->left != NULL) nexhead = head->left;            else nexhead = head->right;            for(last = NULL; head != NULL; last = head, head = findNext(head->next))            {                if(head->left != NULL && head->right != NULL)                    head->left->next = head->right;                if(last == NULL) continue;                if(last->right != NULL)                     last->right->next = head->left != NULL ? head->left : head->right;                else                     last->left->next = head->left != NULL ? head->left : head->right;            }        }    }};

 

转载于:https://www.cnblogs.com/dongguangqing/p/3727925.html

你可能感兴趣的文章
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>