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. Visual C++中对运行时库的支持

    原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 一.什么是C运行时库 1)C运行时库就是 C run-time library,是 ...

  2. Gradle sync failed: Failed to find Build Tools revision 26.0.2的解决办法

    说明在android studio中没有 build tools 的26.0.2的版本,你确认一下,是否是这样: 点击==>android studio的菜单栏中Tools==>andro ...

  3. MyBatis的架构设计以及实例分析

            MyBatis是目前非常流行的ORM框架,它的功能很强大,然而其实现却比较简单.优雅.本文主要讲述MyBatis的架构设计思路,并且讨论MyBatis的几个核心部件,然后结合一个sel ...

  4. openstack中间件message queue 与memcached环境部署

    为什么要安装中间件 组件间的通信使用的是REST API 而组件内部之间的通信则是使用的中间件 首先登陆openstack的官网查看官方文档 www.openstack.org 应为在部署一个架构之前 ...

  5. [SHOI2012]回家的路 最短路

    ---题面--- 题解: 吐槽:找了好久的错,换了n种方法,重构一次代码,,,, 最后发现,,, 数组开小了,其实一开始尝试开大了数组,但唯独没有尝试开大手写队列的数组.... 思路: 有两种方法,这 ...

  6. BZOJ4552:[HEOI2016/TJOI2016]排序——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4552 https://www.luogu.org/problemnew/show/P2824 在2 ...

  7. Spring.NET中事务管理【转】

    http://www.cnblogs.com/GoodHelper/archive/2009/11/16/springnet_transaction.html 浏览了下写的比较清楚. 在.NET FC ...

  8. Eclipse配置web开发环境

    eclipse的web配置: eclipse:Eclipse Java EE IDE for Web Developers. Version: Helios Service Release 1 下载地 ...

  9. 怎样才能快速成为JavaScript高手

    如何快速成为JavaScript高手?之前我拿这个问题问过我的同事,也问过国内的一些JavaScript高手. 最近,我一直在拿这个问题问自己.之所以会有这个问题,我基于两个前提:第一.我自认为自己不 ...

  10. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...