zoukankan      html  css  js  c++  java
  • Leetcode: 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

    复杂度

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

    思路

    相当于是Level Order遍历二叉树。通过一个Queue来控制每层的遍历,注意处理该层最后一个节点的特殊情况。此方法同样可解第二题。

     1 /**
     2  * Definition for binary tree with next pointer.
     3  * public class TreeLinkNode {
     4  *     int val;
     5  *     TreeLinkNode left, right, next;
     6  *     TreeLinkNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public void connect(TreeLinkNode root) {
    11         TreeLinkNode pre = null;
    12         if (root == null) return;
    13         Queue<TreeLinkNode> queue = new LinkedList<>();
    14         queue.offer(root);
    15         while (!queue.isEmpty()) {
    16             int size = queue.size();
    17             for (int i=0; i<size; i++) {
    18                 TreeLinkNode cur = queue.poll();
    19                 if (pre == null) pre = cur;
    20                 else {
    21                     pre.next = cur;
    22                     pre = cur;
    23                 }
    24                 if (cur.left != null) queue.offer(cur.left);
    25                 if (cur.right != null) queue.offer(cur.right);
    26             }
    27             pre = null;
    28         }
    29     }
    30 }

    没想到的方法:因为guarantee了是perfect binary tree

    层次递进法

    复杂度

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

     1 public class Solution {
     2     public void connect(TreeLinkNode root) {
     3         if(root==null) return;
     4         TreeLinkNode cur = root;
     5         TreeLinkNode nextLeftmost = null;
     6 
     7         while(cur.left!=null){
     8             nextLeftmost = cur.left; // save the start of next level
     9             while(cur!=null){
    10                 cur.left.next=cur.right;
    11                 cur.right.next = cur.next==null? null : cur.next.left;
    12                 cur=cur.next;
    13             }
    14             cur=nextLeftmost;  // point to next level 
    15         }
    16     }
    17 }
  • 相关阅读:
    python+OpenCV 特征点检测
    如何使用《DB 查询分析器》高效地生成旬报货运量数据
    Hive-RCFile文件存储格式
    NET:交换机的背板带宽,交换容量,包转发率区别
    css:cdata
    [ZOJ 3623] Battle Ships
    [NYOJ 860] 又见01背包
    [Uva 11825] Hackers’ Crackdown
    2014-10-29
    [转] 三鲜之所在
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978458.html
Copyright © 2011-2022 走看看