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. bzoj1086-王室联邦

    题目 给出一棵树,求一种分块方案,使得每个块的大小\(size\in [B,3B]\).每个块还要选一个省会,省会可以在块外,但是省会到块内任何一个点路径上的所有除了省会的点都必须属于这个块.\(n\ ...

  2. 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流

    题目描述 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain m ...

  3. CentOS 双网卡绑定实现平衡负载

    绑定两块网卡主要为了解决网卡故障.负载均衡等问题. 1.在vm加一块网卡,登录后检查网卡是否识别. 分别用ip addr和nmcli查看网卡的情况 [root@bigdata-senior01 ~]# ...

  4. syslog服务器配置笔记

    syslog服务器可以用作一个网络中的日志监控中心,rsyslog是一个开源工具,被广泛用于Linux系统以通过TCP/UDP协议转发或接收日志消息.本文我们来讲讲在 Linux 上配置一个 sysl ...

  5. Redux的State不应该全部放在Store里

    使用了redux管理应用的状态,应用的状态不应该全部放在Store里面. 前端状态主要有一下两种: 1. Domain data 2. UI State 1. Domain data 来自于服务端对领 ...

  6. bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)

    CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...

  7. 开发系统级应用不被Kill

    一.设置方法 (1) 在AndroidManifest中application根节点下,添加如下代码: android:persistent="true" (2) 将应用程序pus ...

  8. [ 转载]Tomcat7 catalina.out 日志分割

    http://m.blog.csdn.net/blog/mark_qi/8864644 最近由于工作需要,tomcat 的catalina.out文件的不断扩大,导致系统磁盘空间边变小,而且管理也难于 ...

  9. telnet退出

    windows下退出telnet:可以参考下面linux退出,也可以直接关闭窗口. linux退出telnet: 1.输入ctrl+]:显示出telnet>. 2.此时可以输入?,查看可以使用的 ...

  10. poj 2541 Binary Witch

    Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS   Memory Limit: 65536K       Descript ...