[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 * ...
随机推荐
- 在Vue项目里面使用d3.js
之前写一个 Demo里面 有些东西要使用d3实现一些效果 但是在很多论坛找资源都找不到可以在Vue里面使用D3.js的方法,npm 上面的D3相对来说 可以说是很不人性化了 完全没有说 在webpac ...
- Hadoop(14)-MapReduce框架原理-切片机制
1.FileInputFormat切片机制 切片机制 比如一个文件夹下有5个小文件,切片时会切5个片,而不是一个片 案例分析 2.FileInputFormat切片大小的参数配置 源码中计算切片大小的 ...
- C语言实现计算二进制数字1的个数
#include<stdio.h> #include<stdlib.h> int print_one_bits01(unsigned int value){ //0000 11 ...
- 算法竞赛入门经典-1.5.4 Q&A
这小节考察实践能力,要求在不要查书.不要网上找答案,自己用实验的方法解决以下五个问题: 做这五道题时,好几道都没思路,违反了规则到网上找了一圈,居然没找到答案,于是打算写这篇博客.不知是否有更好的实践 ...
- python2.7入门---列表(List)
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推.Python有6个序列的内置类型,但最常见的是列表和元组. ...
- JQuery中的load()、$.get()和$.post()详解 (转)
load() 1.载入HTML文档 load()方法是jQuery中最为简单和常用的Ajax方法,能载入远程HTML代码并插入DOM中. 它的结构为: load(url [,data][,callba ...
- P2347 砝码称重
P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...
- allegro导入网表过程中出现的错误信息
1. 找不到焊盘PAD,下面这句话的意思是器件封装找不到焊盘46.pad WARNING(SPMHNI-): Unable to load symbol ): Could not find padst ...
- elasticsearch-mathc和term的区分
elasticsearch和mysql在思想上是有不同的,elasticsearch有分词一说,比如北京奥运分词成北京,奥运,北京奥运.分词要要考虑两点,一个是查询字符串要不要分词,还有就是原存储字段 ...
- Linux下的调试工具
Linux下的调试工具 随着XP的流行,人们越来越注重软件的前期设计.后期的实现,以及贯穿于其中的测试工作,经过这个过程出来的自然是高质量的软件.甚至有人声称XP会淘汰调试器!这当然是有一定道理的,然 ...