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 }
  • 相关阅读:
    mogodb 设置用户名密码认证
    axon mogoconfig
    ListUtils 对 list数据 分组 ,统计,求和 。。。
    jQuery 之 dom操作
    学习Java第二天
    字节跳动spring面试题,你能回答出几个
    CH340芯片选型
    Django的路由转换器的使用
    Vue之cli脚手架
    String中split(regex,limit)方法讲解
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978458.html
Copyright © 2011-2022 走看看