Populating Next Right Pointers in Each Node 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/


Description

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).

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

Solution


class Solution {
private:
void connectNode(vector<TreeLinkNode*>& v) {
int size = v.size();
for (int i = 0; i <= size - 2; i++) {
v[i] -> next = v[i + 1];
}
}
public:
void connect(TreeLinkNode *root) {
if (root == NULL)
return;
int levelNodeNum = 1;
int curLevelNodeNum = 0;
queue<TreeLinkNode*> q;
vector<TreeLinkNode*> v;
q.push(root);
while (!q.empty()) {
TreeLinkNode* node = q.front();
q.pop();
v.push_back(node);
if (node -> left != NULL)
q.push(node -> left);
if (node -> right != NULL)
q.push(node -> right);
curLevelNodeNum++;
if (curLevelNodeNum == levelNodeNum) {
levelNodeNum *= 2;
curLevelNodeNum = 0;
connectNode(v);
v.clear();
}
}
}
};

解题描述

这道题是关于二叉树层次遍历问题的变种。题目给出的二叉树是完全二叉树,所以可以提前算出每一层的节点数目,因此来说还是相对比较容易的。所以基本的解决办法是,使用一个队列来存放节点。最开始将根节点加入队列。每次从队首取出一个节点,将其子节点加入队尾。然后使用一个计数变量来计算当前层次上已经加入队列的节点数目。一旦达到该层次的节点数目总和就对该层的节点进行next连接。

[Leetcode Week15]Populating Next Right Pointers in Each Node的更多相关文章

  1. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  2. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. [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 ...

  4. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  5. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  6. [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  7. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  8. 【leetcode】Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  9. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

随机推荐

  1. [剑指Offer] 63.数据流中的中位数

    题目描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. c ...

  2. Delphi实现在数据库中存取图像

    向窗体上添加一个TListBox组件.一个TImage组件和一个TTable组件,设计完成的主界面如图1所示. 图1 主界面 本系统中需要设计一个新的基于Paradox 7的数据库Image.db,图 ...

  3. 【Asp.Net Core】ASP.NET Core 2.0 + EF6 + Linux +MySql混搭

    好消息!特好消息!同时使用ASP.NET Core 2.0和.NET Framework类库还能运行在linux上的方法来啦! 是的,你没有看错!ASP.NET Core 2.0,.NET Frame ...

  4. POJ3347:Kadj Squares——题解

    http://poj.org/problem?id=3347 题目大意:给定一些正方形的边长,让他们尽可能向左以45°角排列(不能互相重合),求在上面看只能看到哪几个正方形. ———————————— ...

  5. webpack优化总结

    1. 分包 将不需要变动的第三方包分离出去, 主要方法有: (1). externals(2). DllPlugin(3). expose-loader(4). ProviderPlugin 2. 拆 ...

  6. Android源码4.4.4_r1下载和编译

    系统:ubuntu 16.04.2 TLS 1.源码下载: sudo apt-get install curl curl https://storage.googleapis.com/git-repo ...

  7. [ZJOI2010]排列计数 (组合计数/dp)

    [ZJOI2010]排列计数 题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有 ...

  8. BZOJ2434: [NOI2011]阿狸的打字机(AC自动机+dfs序+树状数组)

    [NOI2011]阿狸的打字机 题目链接:https://www.luogu.org/problemnew/show/P2414 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. ...

  9. 使用tcpdump监控网络消息发送

    tcpdump是一个用于截取网络分组,并输出分组内容的工具,简单说就是数据包抓包工具.tcpdump凭借强大的功能和灵活的截取策略,使其成为Linux系统下用于网络分析和问题排查的首选工具. tcpd ...

  10. Zabbix Server端配置文件

    Zabbix Server端配置文件说明 # This is a configuration file for Zabbix Server process # To get more informat ...