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. Spring MVC之@RequestBody@ResponseBody详解

    引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...

  2. java从远程服务器获取PDF文件并后台打印(使用pdfFox)

    一.java原生方式打印PDF文件 正反面都打印,还未研究出只打印单面的方法,待解决 public static void printFile(String path) throws Exceptio ...

  3. 【周记:距gdoi43天】

    这个星期切了几道题吧,虽然说还是想让自己搏一搏,但是毕竟自己弱嘛,而且很多东西都还没熟透&不像rausen大神都屠进前100了. 加油吧.

  4. BZOJ1149:[CTSC/APIO2007]风铃——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1149 https://www.luogu.org/problemnew/show/P3621 sb ...

  5. BZOJ1047:[HAOI2007]理想的正方形——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1047 https://www.luogu.org/problemnew/show/P2216#sub ...

  6. Vue项目搭建过程

    环境搭建:mac+nodejs+npm #安装node.js : $ brew install node #安装vue-cil: $ npm install -g vue-cli 注:官网下载安装no ...

  7. PowerDesigner 技巧【3】

    一.PowerDesigner导出所有SQL脚本: 一般的导出SQL脚本只需要下面两个步骤: 1.database->change current DBMS(选择需要导出的数据库类型): 2.d ...

  8. ubuntu16.04命令行模式黑屏解决办法

    ubuntu16.04命令行模式黑屏解决办法 问题描述 在ubuntu上装Nvidia的显卡驱动,需要关闭图形界面才能安装驱动,但是,出现如下情况: 使用“ctrl+alt+F1”命令进入命令行界面是 ...

  9. 2115: [Wc2011] Xor (线性基+dfs)

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 5714  Solved: 2420 题目链接:https://w ...

  10. Android JUnit test

    Android单元测试步骤 1.修改AndroidManifest.xml文件. 添加instrumentation节点.其中name是固定值,targetPackage为需要测试的类所在的包.如:  ...