很自然的推广,假设去掉全然二叉树的条件呢?由于这个条件不是关键,因此不会影响整体的思路。做法依旧是每次找到一层的起点,然后一层一层的走。

假设是全然二叉树的话,每层的起点就是上一层起点的左孩子,兄弟之间的链接也简单,直接找相应父亲的左右孩子就可以。

一般二叉树时,起点应该是上一层第一个有孩子节点的左孩子,或者没有左孩子时。是他的右孩子。

为了能在孩子层中不断链接,我们必须保存当孩子层的前一个节点,当当前层找到一个节点有孩子,就接到这个pre节点后面,然后更新pre节点的指向。

class Solution {
public:
void connect(TreeLinkNode *root) {
if(root == NULL) return;
TreeLinkNode *beginNode = root, *leftNode, *pre;
while(beginNode){
leftNode = beginNode;
pre = NULL;
beginNode = NULL;
while(leftNode){
if(leftNode->left&&leftNode->right){
leftNode->left->next = leftNode->right;
if(pre) pre->next = leftNode->left;
if(!beginNode) beginNode = leftNode->left;
pre = leftNode->right;
}else if(leftNode->left){
if(pre) pre->next = leftNode->left;
if(!beginNode) beginNode = leftNode->left;
pre = leftNode->left;
}else if(leftNode->right){
if(pre) pre->next = leftNode->right;
if(!beginNode) beginNode = leftNode->right;
pre = leftNode->right;
}
leftNode = leftNode->next;
}
}
}
};

leetcode第一刷_Populating Next Right Pointers in Each Node II的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

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

  5. 【LeetCode OJ】Populating Next Right Pointers in Each Node II

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...

  6. LeetCode OJ: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 ...

  7. leetcode第一刷_Best Time to Buy and Sell Stock II

    这道题尽管是上一道题的增强.可是反而简单了. 能够交易无数次,可是买卖必须成对的出现. 为了简单起见.我用abc三股股票来说明,且忽略掉相等的情况.三个数一共同拥有六种大小关系.注意他们之间的先后顺序 ...

  8. LeetCode OJ 117. Populating Next Right Pointers in Each Node II

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

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

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

随机推荐

  1. selenium_Alert

    网页测试,最避免不了的就是弹出框,但是弹出框你真的分的清吗? Alert prompt comfirm 先来认识一下这三个弹窗 代码如下 <!DOCTYPE html> <html ...

  2. 常用的Python代码段

    过滤列表 #filter out empty strings in a sting list list = [x for x in list if x.strip()!=''] 一行一行地读文件 wi ...

  3. C# Ioc容器Unity,简单实用

    开头先吐槽一下博客园超级不好用,添加图片后就写不动字了,难道是bug 好进入正题,先来说下依赖注入,简单来说就是定义好接口,上层代码调用接口,具体实现通过配置文件方式去指定具体实现类. 首先我们需要通 ...

  4. ubuntu server小技巧(不定期更新)

    0.常用工具apt安装包名 # ssh服务器工具 apt-get install openssh-server # RabbitMQapt-get install rabbitmq-server # ...

  5. Nodejs mongodb 管理组件adminmongodb

    强大的 nodejs的mongodb管理工具,强大到即下即用: 安装需求: 1.git命令获取组件包,git clone https://github.com/mrvautin/adminMongo. ...

  6. [转载] ZooKeeper原理及使用

    转载自http://www.wuzesheng.com/?p=2609 ZooKeeper是Hadoop Ecosystem中非常重要的组件,它的主要功能是为分布式系统提供一致性协调(Coordina ...

  7. [转载] ETL和Kettle

    http://tech.ccidnet.com/art/1105/20080407/1411567_1.html http://blog.csdn.net/cissyring/article/deta ...

  8. lua API函数大全

    Lua5.1中的API函数 lua_State* luaL_newstate()Lua脚本的编译执行是相互独立的,在不同的线程上执行.通过luaL_newstate()函数可以申请一个虚拟机,返回指针 ...

  9. Azure cli使用arm创建多网卡虚拟机

    登录 Azure CLI 并使用 Resource Manager 模式: azure config mode arm 在以下示例中,请将示例参数名称替换为你自己的值.示例参数名称包括 myResou ...

  10. Servlet之初始化参数和传递数据(ServletConfig,ServletContext )

    ServletConfig 容器初始化一个Servlet的时候,会为这个Servlet建一个唯一的Servletconfig的对象(Servlet的配置对象) 容器会从部署的描述文件(web.xml) ...