zoukankan      html  css  js  c++  java
  • [LeetCode][JavaScript]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node

    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

     https://leetcode.com/problems/populating-next-right-pointers-in-each-node/


    树的按层遍历,其实就是bfs。

    维护一个队列放每一层的节点,在每一轮中,把这一层的节点按照顺序连起来。

    因为是树的结构,访问过的节点不会再访问,不需要bfs的visited对象。 

    Populating Next Right Pointers in Each Node II同样的代码就能过,但是II要求不能用额外的空间。

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * function TreeLinkNode(val) {
     4  *     this.val = val;
     5  *     this.left = this.right = this.next = null;
     6  * }
     7  */
     8 /**
     9  * @param {TreeLinkNode} root
    10  * @return {void} Do not return anything, modify tree in-place instead.
    11  */
    12 var connect = function(root) {
    13     var queue = [];
    14     queue.push(root);
    15     bfs();
    16 
    17     function bfs(){
    18         if(queue.length > 0){
    19             var top = null;
    20             var newQueue = [];
    21             var preNode = null;
    22             while(top = queue.shift()){
    23                 if(top.left){
    24                     newQueue.push(top.left);
    25                     if(preNode){
    26                         preNode.next = top.left;
    27                     }
    28                     preNode = top.left;
    29                 }
    30                 if(top.right){
    31                     newQueue.push(top.right);
    32                     if(preNode){
    33                         preNode.next = top.right;
    34                     }
    35                     preNode = top.right;
    36                 }
    37             }
    38             if(preNode){
    39                 preNode.next = null;
    40             }
    41             queue = newQueue;
    42             bfs();
    43         }
    44     }
    45 };
  • 相关阅读:
    Visual C++2005的IntelliSense问题
    R调用数据库的方式汇总
    Loading local data is disabled; this must be enabled on both the client and server sides 
    R的transform函数
    R连接mysql数据库(ODBC)
    mysql实例代码
    随笔20130105
    开端
    MeteoInfo
    火车软硬卧铺分布图
  • 原文地址:https://www.cnblogs.com/Liok3187/p/4582146.html
Copyright © 2011-2022 走看看