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

Subscribe to see which companies asked this question

解答

利用已经连好的next指针进行广度优先遍历,注意是每次处理下一层,所以下一层为NULL的时候就不需要处理了。

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  struct TreeLinkNode *left, *right, *next;
 * };
 *
 */
void connect(struct TreeLinkNode *root) {
    struct TreeLinkNode *head, *next_head, *pNode, *pre_node;

    if(NULL == root){
        return;
    }
    root->next = NULL;
    next_head = root;
    ){
        head = next_head;
        next_head = next_head->left;
        if(NULL == next_head){
            break;
        }
        for(pNode = head; pNode != NULL; pNode = pNode->next){
            if(pNode != head){
                pre_node->next = pNode->left;
            }
            pNode->left->next = pNode->right;
            pre_node = pNode->right;
        }
        pre_node->next = NULL;
    }
}

LeetCode OJ 116. Populating Next Right Pointers in Each Node的更多相关文章

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

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

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

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

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

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

  5. 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node

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

  6. 【LeetCode】116. Populating Next Right Pointers in Each Node

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

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

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

  8. LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)

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

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

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

随机推荐

  1. Linux下创建新用户

    useradd -h查询使用方法 useradd -g sysadmin -d /home/devops -m -c "DevOps Installation User" -s / ...

  2. 封装tip控件

    在界面上有时需要显示一个提示,大多的前端框架都把提示做成一个带有小尖角的提示框,因此自己也仿照了实现了一下,效果图如下: 尖角的实现很简单的,一般都是通过css将div的宽高设置为0,然后将尖角朝向的 ...

  3. ORACLE插入DATE类型字段

    1 怎样在ORACLE中输入DATE类型的字段 insert into table_name (date_column) values(to_date('2006-06-04','yyyy-mm-dd ...

  4. adb server无法终止问题

    这两天通过python去连接Android手机时,一直提示:Adb connection Error:An existing connection was forcibly closed by the ...

  5. django--模板(七)

    1.模板加载 一个项目可以有多个应用,每个应用下都可以有模板,思考模板的查找顺序?

  6. 在树莓派2代B型/3代 上安装Fedora23 - Installing Fedora 23 on Raspberry Pi 2 model B or Raspberry Pi 3

    本文主要用于记录安装过程,以便日后查阅回顾. 之前在自己的树莓派上运行的一直是通过NOOB安装的Raspbian,但是本人平时更喜欢用Fedora作为开发和使用环境,而且Raspbian上的软件包通常 ...

  7. AChartEngine 图表绘制

    AChartEngine Android 平台的图表开发库, 能绘制 折线图, 饼图, 气泡图, 柱状图, 散点图, 面积图等统计图表. 这些我记录一下,柱状图.折线图和饼状图的小例子.有兴趣的朋友, ...

  8. 原生js事件和jquery事件的执行顺序问题

    场景:近日,写前端页面时候,在针对输入框input操作时,用到了jquery的插件,插件中使用了jquery的focus()和blur()方法.但是同时,又需要在插件之外再针对输入框的获取焦点和失去焦 ...

  9. Upload files to aliyunOSS with bootstrap-fileinput

    本文主要涉及两个概念: 阿里云OSS:对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量.安全和高可靠的云存储服务. bootstrap-fileinput: ...

  10. 登录框密码框input框禁止复制、粘贴、剪切和右键功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...