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

    Populating Next Right Pointers in Each Node

    问题:

    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)

    思路:

      采用队列方法,poll队首的同时,加入两个其孩子

      核心是记录每一个层次的大小,两层循环,时间复杂度为O(n)

    我的代码:

    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root == null)
                return ;
            LinkedList<TreeLinkNode> ll = new LinkedList<TreeLinkNode>() ;
            ll.add(root) ;
            while(!ll.isEmpty())
            {
                int size = ll.size() ;
                TreeLinkNode pre = ll.poll() ;
                if(pre.left != null)
                    ll.add(pre.left) ;
                if(pre.right != null)
                    ll.add(pre.right) ;
               for(int i = 0 ; i < size - 1 ; i++)
               {
                   TreeLinkNode tmp = ll.poll() ;
                   pre.next = tmp ;
                   if(tmp.left != null)
                   {
                       ll.add(tmp.left) ;
                   }
                   if(tmp.right != null)
                   {
                        ll.add(tmp.right) ;     
                   }
                   pre = pre.next ;
               }
               pre.next = null ;
            }
        }
    }
    View Code

    他人代码:

        public void connect(TreeLinkNode root) {
            if (root == null) {
                return;
            }
            
            TreeLinkNode leftEnd = root;
            while (leftEnd != null && leftEnd.left != null) {
                TreeLinkNode cur = leftEnd;
                while (cur != null) {
                    cur.left.next = cur.right;
                    cur.right.next = cur.next == null ? null: cur.next.left;
                    
                    cur = cur.next;
                }
                
                leftEnd = leftEnd.left;
            }
        }
    View Code

    学习之处:

    • 自己的方法不足之处在于空间复杂度为O(n),而他人方法的空间复杂度为O(1)
    • 他人方法的思路为使用2个循环,一个指针P1专门记录每一层的最左边节点,另一个指针P2扫描本层,把下一层的链接上

    if(null == root) return ;
    LinkedList<TreeLinkNode> nodeQueue = new LinkedList<TreeLinkNode>();
    nodeQueue.add(root);

    while(!nodeQueue.isEmpty()){
    int size = nodeQueue.size();
    TreeLinkNode pre = nodeQueue.poll();
    if(null != pre.left) nodeQueue.add(pre.left);
    if(null != pre.right) nodeQueue.add(pre.right);
    for(int i=0; i<size-1; i++){
    TreeLinkNode cur = nodeQueue.poll();
    if(null != cur.left) nodeQueue.add(cur.left);
    if(null != cur.right) nodeQueue.add(cur.right);
    pre.next = cur;
    pre = cur;
    }
    pre.next = null;
    }

  • 相关阅读:
    Promises-小程序购物车结算
    微信小程序支付
    微信企业付款到零钱
    Ubuntu16 远程连接MySQL
    MySQL数据库中文变问号
    Ubuntu开启ApacheRewrite功能
    大数据2018:云存储已在客观层面扮演数据湖角色
    地平线发布兼具本地端抓拍与识别功能的嵌入式AI摄像机
    肥皂遇上黑科技!异味统统都走开!
    LG新专利或用于移动VR,可通过外部旋钮调节显示屏与透镜
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4313511.html
Copyright © 2011-2022 走看看