[Leetcode] Populating next right pointer 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 题中要求二叉树为完美二叉树(满二叉树,Perfect binary tree),其性质为一个深度为k(>=-1)且有2^(k+1) - 1个结点。通俗的讲,叶节点处于同一层,除叶节点以外其他结点均有左右孩子。详情见博友veli的博客。题目要求用常数O(1)空间,故不能用队列等。
要是能用队列,则可以用层次遍历中的方法三,然后每次从queue中取出一个元素时,将其next指针指向queue中下一个节点即可,可惜不满足题意。
因为,不能用常规的队列解决问题,所以遇到几个难点。一、如何构成循环;二、如何从一个结点的左孩子转到右孩子。网友们给出了解题思路,非本人原创,这里仅给出自己的理解。
思路:类似的层次遍历思想,用firLeft记录下每层的最左端的结点,然后从左往右遍历,值得注意的是,每层的最右端元素的next不必重新指向NULL ,因为,二叉树的结构体中,每个结点的next是自动赋为NULL的。这就产生一个问题:如何从一层转向下一层,即循环条件。因为,二叉树为完美二叉树,
一般结点的(非叶节点)的左右孩子都是存在的,故可以以结点的左孩子或者右孩子是否存在来为条件;二是,如何在层内移动,核心思想为,用上一层的节点控制下一层的指向。对某一节点,其左孩子直接指向右孩子即可,节点之间,其next若是存在,则用该结点的右孩子指向其next的左孩子。最后
firLeft=firLeft->left即可。
/**
* 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; TreeLinkNode *firLeft=root;
while(firLeft->left)
{
TreeLinkNode *parNode=firLeft;
while(parNode)
{
parNode->left->next=parNode->right;
if(parNode->next)
parNode->right->next=parNode->next->left; parNode=parNode->next;
}
firLeft=firLeft->left;
}
}
};
方法二:递归,见Grandyang的博客
// Recursion, more than constant space
class Solution {
public:
void connect(TreeLinkNode *root) {
if (!root) return;
if (root->left) root->left->next = root->right;
if (root->right) root->right->next = root->next? root->next->left : NULL;
connect(root->left);
connect(root->right);
}
};
[Leetcode] Populating next right pointer in each node 填充每个节点的右指针的更多相关文章
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- LeetCode: Populating Next Right Pointer in Each Node
LeetCode: Populating Next Right Pointer in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 117 Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
这是“每个节点的右向指针”问题的进阶.如果给定的树可以是任何二叉树,该怎么办?你以前的解决方案仍然有效吗?注意: 你只能使用恒定的空间.例如,给定以下二叉树, 1 / ...
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
随机推荐
- Laravel 5.5搭建(lunix-ubuntu)
基本配置 PHP >= 7.0.0 PHP OpenSSL 扩展 PHP PDO 扩展 PHP Tokenizer 扩展 PHP XML 扩展 1:nginx sudo apt-get upda ...
- python应用:TXT文件的读写
python读写TXT文件不需要导入包 python中常用的读写方式: 文件打开模式 描述 r 以只读模式打开文件,并将文件指针指向文件头:如果文件不存在会报错 w 以只写模式打开文件,并将文件指针指 ...
- python中如何统计一个类的实例化对象
类中的静态变量 需要通过类名.静态变量名 来修改 :通过对象不能修改 python中如何统计一个类的实例化对象?? class Person: #静态变量count,用于记录类被实例化的次数 coun ...
- python数据类型及其特有方法
一.运算符 in方法 "hello" in "abcdefghijklmnopqrstuvwxyz" "li" in ["gg&q ...
- Leecode刷题之旅-C语言/python-53.最大子序和
/* * @lc app=leetcode.cn id=53 lang=c * * [53] 最大子序和 * * https://leetcode-cn.com/problems/maximum-su ...
- Qt之pro文件解析
在我们创建Qt工程项目时,Qt Creator总会创建一个.pro文件,我们称.pro文件为Qt的工程管理文件.一个工程项目可以包含一个或多个.pro文件.理解和掌握pro文件的用法,将有利于Qt开发 ...
- Infinite Maze CodeForces - 196B
We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...
- ABAP CDS ON HANA-(11)ABAP CDSでの関連付け
Association in ABAP CDS An association in CDS view joins different data sources. Defining and using ...
- linux execl()函数
关于execl()函数族的用法不在赘述,其他博主介绍的很详细.下面说下作者在使用该函数时所犯的错误: 作者想通过使用execl()函数在子进程中调用其他函数,起初楼主是 这样用的: if((a = e ...
- 数据库 MySQL part1
数据库介绍 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性,并可为各种 ...