题目:

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

For 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 题目给出的二叉树是完美二叉树,要求是将每层的结点依次连接起来,并且每层的最后一个结点的next赋为null。 思路:
首先将每一层的最后一个结点的next赋值为null。
然后依次操作每一层,首先获取每一层的第一个结点。(其实二叉树和链表有点像,完美二叉树的最外两侧就可以看做两张链表,left和right指针类似链表的next指针)
引入两个指针,分别是cur和pre。 cur指向当前操作层,pre指向上一层。
1.cur是pre的左孩子,直接将cur的next指向pre的right结点,cur变成cur->next;
2.cur是pre的右孩子,并且pre的next不为null,cur->next指向pre->next->left;
3.cur是pre的右孩子,并且pre的next为null,说明cur已经指向了该层的最后一个结点,直接将cur->next赋为null即可。
执行到此处,要更新pre和cur,让它们分别指向自己下一层的第一个结点。(引入计数变量count,每次执行到此处count加1,根据count更新pre和cur)
循环结束条件:cur为null。 代码:
     public void connect(TreeLinkNode root){
if(root == null){
return;
} TreeLinkNode cur = root, pre = root;
//把每层最右侧的结点的next赋为null
while(cur != null){
cur.next = null;
cur = cur.right;
}
int count = 0;
cur = root.left;
while(true){
if(cur == pre.left){
cur.next = pre.right;
cur = cur.next;
}else{
if(cur == pre.right && pre.next != null){
cur.next = pre.next.left;
cur = cur.next;
pre = pre.next;
}else{
cur.next = null;
count ++;
pre = root;
for(int i = 0; i < count; i++){
pre = pre.left;
}
cur = pre.left;
}
}
} }

 

leetcode 116- Populating Next Right Pointers in Each Node的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

  3. leetcode 116 Populating Next Right Pointers in Each Node ----- java

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

  4. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

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

  5. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

  6. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

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

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

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

  9. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

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

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

随机推荐

  1. A20板子上的触摸屏设备号变化后解决

  2. TCP/IP简介

    TCP/IP协议分层 提到协议分层,我们很容易联想到ISO-OSI的七层协议经典架构,但是TCP/IP协议族的结构则稍有不同.如图所示 TCP/IP协议族按照层次由上到下,层层包装.最上面的就是应用层 ...

  3. HTML: 用CSS畫一個三角形

    代碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  4. DOS实用命令/工具

    winver  检查Windows版本wmimgmt.msc  打开windows管理体系结构wupdmgr  windows更新程序wscript  windows脚本宿主设置write  写字板w ...

  5. vbox共享文件 挂载

    环境:主机操作系统是Windows 7,虚拟机是open suse 12.0,虚拟机是VirtualBox 4.2.1. 1. 安装增强功能包(Guest Additions) 安装好open sus ...

  6. js文档视口高度函数

    objwin=window;objBody=document.body;objDel=document.documentElement;   关于弹窗时候用到 function getPageHeig ...

  7. correctly handle PNG transparency in Win IE 5.5 & 6.

    function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var arVersion = ...

  8. Python 链接Mysql数据库

    参考链接:https://pypi.python.org/pypi/PyMySQL#downloads import pymysql.cursors,xml.dom.minidom # Connect ...

  9. php-- Linux图形界面与字符界面切换

    转自:http://blog.163.com/wang_ly2442/blog/static/9494340720128355054551/ 1. 启动时进入字符界面,后来想切换到图形界面:使用sta ...

  10. ubuntu下的jdk安装

    软件环境: 虚拟机:VMware Workstation 10 操作系统:ubuntu-12.04-desktop-amd64 JAVA版本:jdk-7u55-linux-x64 软件下载地址: JD ...