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连接。

转载于:https://www.cnblogs.com/yanhewu/p/8041064.html

[Leetcode Week15]Populating Next Right Pointers in Each Node相关推荐

  1. 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II

    LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...

  2. LeetCode 116. Populating Next Right Pointers in Each Node

    LeetCode 116. Populating Next Right Pointers in Each Node Solution1:我的答案 迭代版层次遍历 有个2B bug困扰了我好久 clas ...

  3. LeetCode 117. Populating Next Right Pointers in Each Node II

    原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...

  4. LeetCode OJ - Populating Next Right Pointers in Each Node II

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

  5. [Leetcode][JAVA] 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 @ python

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

  7. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  8. Leetcode: Populating Next Right Pointers in Each Node II

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

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

最新文章

  1. centos7精简安装后使用发现没有killall命令
  2. js中event对象属性和方法
  3. 数据中心继续蓬勃发展的5个原因
  4. 只能选择分卷文件的第一部分。_为机器学习模型选择正确的度量评估(第一部分)...
  5. 讯飞C/C++语音合成基础篇
  6. 用run as以管理员权限运行脚本的方法
  7. php中颜色的索引值,PHP imagecolorsforindex - 取得某索引的颜色
  8. TensorFlow2.0:自定义层与自定义网络
  9. git 入门操作指令
  10. SpringBoot AOP 理解和用途
  11. NetBeans 时事通讯(刊号 # 80 - Nov 15, 2009)
  12. python ndimage_Python ndimage.zoom方法代码示例
  13. 微服务架构下的统一身份认证和授权
  14. python的镜像安装和设置
  15. 在 AIX 上实现 iSCSI
  16. Oracle 11g 中恢复管理器RMAN介绍
  17. <Android>布局中gravity和layout_gravity区别
  18. Pytorch的model.train() model.eval() torch.no_grad() 为什么测试的时候不调用loss.backward()计算梯度还要关闭梯度
  19. Etag与HTTP缓存机制
  20. Elasticsearch:Elastic Maps 现在支持机器学习异常层

热门文章

  1. XP下使用FFMPEG(API和exe)遇到的问题和解决方法。
  2. 线程其实就是一个个指令组成的,当这个线程内的指令全部执行完了,那么这个线程也就执行结束了
  3. 接oracle私活价格,也来记录一下第一次接私活的体验
  4. mysql漏洞包_MySQL npm包中的本地文件泄露漏洞
  5. 操作系统:升级Windows 11正式版的四种方法,值得收藏!
  6. 【译】程序员都有的这 10 个坏习惯!
  7. 项目测试基础:白盒测试相关知识笔记
  8. 算法基础:递归算法知识笔记
  9. 2020最新版SpringMVC面试题高频精选
  10. linq.js的用法