zoukankan      html  css  js  c++  java
  • Populating Next Right Pointers in Each Node(I and II)

    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
    

    将next设置为指向每一层的下一个节点。这样首先想到可以使用广度优先遍历(层序),也是对每一层处理,这里在处理每一层的元素时,只要不是一层的最后一个节点,它的next就是队列的下一个输出元素。见代码:

    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root==null) return ;
            Queue<TreeLinkNode> q=new LinkedList<>();
            q.add(root);
            while(!q.isEmpty()){
                int size=q.size();
                for(int i=0;i<size;i++){
                    TreeLinkNode node=q.poll();
                    if(node.left!=null) q.add(node.left);
                    if(node.right!=null) q.add(node.right);
                    if(i==size-1){   //每层的最后一个,将next指向null
                        node.next=null;
                    }else{//将指针指向下一次准备输出的节点.
                        node.next=q.peek();
                    }
                }
            }
        
           
            
        }
    }

    上面的方法很好,但是使用了一个额外的空间队列,这里不使用额外空间queue。

    这里是深度优先遍历。观察可以看出,遍历每一层从左往右,给他们的下一层赋值next。具体看代码:

            TreeLinkNode level_start=root;
            while(level_start!=null){  //每一层的最左边
                TreeLinkNode cur=level_start;//用于在一层中向右遍历
                //给下一层赋值next
                while(cur!=null){
                    
                    if(cur.left!=null) cur.left.next=cur.right;
                    if(cur.right!=null&&cur.next!=null) cur.right.next=cur.next.left;//当next为空,则不作操作,默认cur.right.next=null
                    cur=cur.next;
                }
               level_start=level_start.left; 

    II 

           1
           /  
          2    3
         /     
        4   5    7

    第二个题目是一样的,就是不是一颗满二叉树,是任意的。上面的队列方法还是一样可以做,代码一样。。而没用queue的方法我就不掌握了。

     
  • 相关阅读:
    改造MFC程序,使原来不支持winsocket的工程支持winsocket
    算术移位和逻辑移位实现分析
    MFC 编辑框中字体大小改变,行高不能改变,只能显示一半的问题,已解决。
    在MFC中,使用控制台Console输出调试信息
    在MFC中使用GDI+的一般方法,以VC6.0编译器为例
    WinForm 实现主程序(exe)与其关联类库(*.dll)分开存放
    Deserializing/Serializing SOAP Messages in C#
    List分页
    ConvertJavaMiliSecondToDateTime
    中文数字大小写转阿拉伯数字
  • 原文地址:https://www.cnblogs.com/xiaolovewei/p/8267066.html
Copyright © 2011-2022 走看看