Populating Next Right Pointers in Each Node II 题解

原创文章,拒绝转载

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


Description

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

What if the given tree could be any binary tree? Would your previous solution still work?

Note: You may only use constant extra space.

Example

Given the following binary tree,


1
/ \
2 3
/ \ \
4 5 7

After calling your function, the tree should look like:


1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 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];
}
} struct LevelNode {
TreeLinkNode* node;
int level;
LevelNode(TreeLinkNode* n, int l) {
node = n;
level = l;
}
}; const int InitLevel = 1;
public:
void connect(TreeLinkNode *root) {
if (root == NULL)
return;
int curLevel = InitLevel;
vector<TreeLinkNode*> v;
queue<LevelNode> q;
q.push(LevelNode(root, InitLevel));
while (!q.empty()) {
LevelNode ln = q.front();
q.pop();
if (ln.node -> left != NULL) {
q.push(LevelNode(ln.node -> left, ln.level + 1));
}
if (ln.node -> right != NULL) {
q.push(LevelNode(ln.node -> right, ln.level + 1));
}
if (ln.level != curLevel) {
connectNode(v);
v.clear();
curLevel++;
}
v.push_back(ln.node);
}
connectNode(v);
}
};

解题描述

这道题是Populating Next Right Pointers in Each Node的升级版:这道题里面的二叉树不是完全二叉树,所以每一层的节点数目没有办法提前计算。我想到的算法是,使用一个新的结构体来记录队列中的节点,然后这个结构体中包含另外一个属性——即层次编号。通过层次编号来区分不同层次的节点,在层次编号发生变化的时候对当前层次记录表中的节点进行next连接即可。

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

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

  2. 【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 ...

  3. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

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

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

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

  7. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

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

  8. Leetcode 之Populating Next Right Pointers in Each Node II(51)

    void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...

  9. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

随机推荐

  1. timer实现

    实现一个 timer 前段时间写过一篇 blog 谈到 用 timer 驱动游戏 的一个想法.当 timer 被大量使用之后,似乎自己实现一个 timer 比用系统提供的要放心一些.最近在重构以前的代 ...

  2. JSON字符串书写

      { "XXX公司": [ { "name": "IT部", "mebers": [ { "维护人员&quo ...

  3. 复杂类型的write写入功能 步骤解析

  4. 2011 Multi-University Training Contest 6 - Host by JLU

    打了4hours,做出一道题...太菜了.rank:45/107 开场看B,题目看不懂...3hours半才发现i<=N-1,不是i<=x-1.然而还是不会. 看到J有人过了,发现是个简单 ...

  5. hdu 2151 Worm (DP)

    Worm Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. CSS-posiziton

    1. 想要实现,”返回顶部”永远位于页面的右下角.需要用到position函数.CSS:层叠样式表.用到了分层的功能. position:fixed;  永远固定在一个地方. <!DOCTYPE ...

  7. MySQL中数据表的基本操纵

    本文基于对国家863中部软件孵化器编著的<MySQL从入门到精通>一书的操作实践.  一.创建数据表 数据表属于数据库,在创建数据表之前,应该使用语句 USE 数据库名  指定操作是在那个 ...

  8. ucenter搭建

    使用xftp传到虚拟机.解压[root@ygy130 ~]# unzip -o -d ./Ucenter_1.6 UCenter_1.6.0_SC_UTF8.zip [root@ygy130 ~]# ...

  9. Nginx漏洞利用与安全加固

    本文主要分为两大部分,第一部分介绍了Nginx的一些常见安全漏洞的形成原因.利用方法,并给出了相应的解决办法;第二部分介绍了Nginx安全加固时需要关注的主要内容. Nginx(发音同engine x ...

  10. caffe环境的搭建(Ubuntu14.04 64bit,无CUDA,caffe在CPU下运行)

    1. 安装BLAS : $ sudo apt-get install libatlas-base-dev 2. 安装依赖项: $ sudo apt-get install libprotobuf-de ...