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 };
  • 相关阅读:
    hdu 1176 免费馅饼
    http://codeforces.com/contest/741/problem/B B. Arpa's weak amphitheater and Mehrdad's valuable Hoses
    瞎搞题
    1D1D决策单调性dp
    整体二分(POJ2104 静态第k大查询)
    B
    http://codeforces.com/contest/776/problem/G
    http://codeforces.com/contest/776/problem/D 2-sat
    bzoj1492(cdq分治 && 平衡树维护dp)
    F. Bear and Bowling 4(斜率优化)
  • 原文地址:https://www.cnblogs.com/Liok3187/p/4582146.html
Copyright © 2011-2022 走看看