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 }
  • 相关阅读:
    免费公共dns推荐
    vm10 mac 序列号
    sublimetext 序列号
    操作系统第6次实验报告:使用信号量解决进程互斥访问
    操作系统第5次实验报告:内存管理
    操作系统第4次实验报告:文件系统
    操作系统第3次实验报告:管道
    操作系统第2次实验报告:创建进程
    OS第1次实验报告:熟悉使用Linux命令和剖析ps命令
    第四次实验报告:使用Packet Tracer理解RIP路由协议
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978458.html
Copyright © 2011-2022 走看看