zoukankan      html  css  js  c++  java
  • Leetcode: Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node".
    
    What if the given tree could be any binary tree? Would your previous solution still work?
    
    Note:
    
    You may only use constant extra space.
    For example,
    Given the following binary tree,
             1
           /  
          2    3
         /     
        4   5    7
    After calling your function, the tree should look like:
             1 -> NULL
           /  
          2 -> 3 -> NULL
         /     
        4-> 5 -> 7 -> NULL
     1 class Solution {
     2     public Node connect(Node root) {
     3         Node pre = null;
     4         if (root == null) return null;
     5         Queue<Node> queue = new LinkedList<>();
     6         queue.offer(root);
     7         while (!queue.isEmpty()) {
     8             int size = queue.size();
     9             for (int i = 0; i < size; i++) {
    10                 Node cur = queue.poll();
    11                 if (pre == null) pre = cur;
    12                 else {
    13                     pre.next = cur;
    14                     pre = cur;
    15                 }
    16                 if (cur.left != null) queue.offer(cur.left);
    17                 if (cur.right != null) queue.offer(cur.right);
    18             }
    19             pre = null;
    20         }
    21         return root;
    22     }
    23 }

    层次递进法

    复杂度

    时间 O(N) 空间 O(1)

     1 public class Solution {
     2     
     3     //based on level order traversal
     4     public void connect(TreeLinkNode root) {
     5 
     6         TreeLinkNode head = null; //head of the next level
     7         TreeLinkNode prev = null; //the leading node on the next level
     8         TreeLinkNode cur = root;  //current node of current level
     9 
    10         while (cur != null) {
    11             
    12             while (cur != null) { //iterate on the current level
    13                 //left child
    14                 if (cur.left != null) {
    15                     if (prev != null) {
    16                         prev.next = cur.left;
    17                     } else {
    18                         head = cur.left;
    19                     }
    20                     prev = cur.left;
    21                 }
    22                 //right child
    23                 if (cur.right != null) {
    24                     if (prev != null) {
    25                         prev.next = cur.right;
    26                     } else {
    27                         head = cur.right;
    28                     }
    29                     prev = cur.right;
    30                 }
    31                 //move to next node
    32                 cur = cur.next;
    33             }
    34             
    35             //move to next level
    36             cur = head;
    37             head = null;
    38             prev = null;
    39         }
    40         
    41     }
    42 }
  • 相关阅读:
    联合索引和多个单列索引选择
    CentOS6.5 一台服务器同时安装多个Mysql数据库
    一次CentOS的服务器被攻击教训
    java版本的memcache静态化
    mysql存储空间满的处理方式
    MariaDB 10.0 和 MariaDB 10.1 存储过程中 PREPARE FROM EXECUTE 区别
    CentOS6.x 优化脚本
    Mysql 使用 “LOAD DATA INFILE”需要注意的问题
    Mysql 日期类型比较 TIMESTAMPDIFF
    CentOS6.x 源码安装Nginx
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978460.html
Copyright © 2011-2022 走看看