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 }
  • 相关阅读:
    为什么 PCB 生产时推荐出 Gerber 给工厂?
    Fedora Redhat Centos 有什么区别和关系?
    【KiCad】 如何给元件给元件的管脚加上划线?
    MCU ADC 进入 PD 模式后出现错误的值?
    FastAdmin 生产环境升级注意
    EMC EMI 自行评估记录
    如何让你的 KiCad 在缩放时不眩晕?
    KiCad 5.1.0 正式版终于发布
    一次单片机 SFR 页引发的“事故”
    java基础之集合
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978458.html
Copyright © 2011-2022 走看看