zoukankan      html  css  js  c++  java
  • [LeetCode]:116: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
    

    思路1:使用深度优先搜索遍历

    代码:

        public static void connect_DFS(TreeLinkNode root) {
            if(root != null){
                if(root.left != null && root.right != null){
                    root.left.next = root.right;
                    System.out.println(root.left.val +" Next : "+ root.right.val);
                    if(root.left.right != null && root.right.left != null){
                        root.left.right.next =  root.right.left;
                        System.out.println(root.left.right.val +" Next : "+ root.right.left.val);
                    }
                }
                
                if(root.left != null ){
                    connect_DFS(root.left);
                }
                if(root.right != null ){
                    connect_DFS(root.right);
                }
            }
        }

    分析:存在缺点:第一左子树的最右边的节点的情况处理不到,分析貌似用深搜不行,后来看了网上高人的解答,发现:root.right.next =  root.next.left

    更改代码为:

    public class Solution {
        public static void connect(TreeLinkNode root) {
            if(root != null){
                if(root.left != null && root.right != null){
                    root.left.next = root.right;
                    //System.out.println(root.left.val +" Next : "+ root.right.val);
                    
                    if(root.next!= null){
                        root.right.next =  root.next.left;
                        //System.out.println(root.right.val +" Next : "+ root.next.left.val);
                    }
                }
                
                if(root.left != null ){
                    connect(root.left);
                }
                if(root.right != null ){
                    connect(root.right);
                }
            }
        }
    }

    思路二:采用广度优先搜索

    代码:

    /**
     * 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 static void connect(TreeLinkNode root) {
            if(root!= null){
                Queue<TreeLinkNode> queListFlag = new LinkedList<TreeLinkNode>();
                
                queListFlag.add(root);
    
                while(queListFlag.size()!=0){
                    ArrayList arrayTemp=new ArrayList();
    
                    while(queListFlag.size()!=0){
                        arrayTemp.add(queListFlag.peek());
                        queListFlag.remove();
                    }
                    
                    if(arrayTemp.size()==1){
                        TreeLinkNode NodeTemp = (TreeLinkNode)arrayTemp.get(0);
                        
                        if(NodeTemp.left != null && NodeTemp.right != null){
                            NodeTemp.left.next = NodeTemp.right;
                            //System.out.println(NodeTemp.left.val +" Next : "+ NodeTemp.right.val);
                        }
                        
                        if(NodeTemp.left != null ){
                            queListFlag.add(root.left);
                        }
                        if(NodeTemp.right != null ){
                            queListFlag.add(root.right);
                        } 
                    }else if(arrayTemp.size()>1){
                        for(int i=0;i<arrayTemp.size()-1;i++) {
                            TreeLinkNode NodeTemp1 = (TreeLinkNode)arrayTemp.get(i);
                            TreeLinkNode NodeTemp2 = (TreeLinkNode)arrayTemp.get(i+1);
                            
                            if(NodeTemp1.left != null && NodeTemp1.right != null){
                                NodeTemp1.left.next = NodeTemp1.right;
                                //System.out.println(NodeTemp1.left.val +" Next : "+ NodeTemp1.right.val);
                            }
                            
                            if(NodeTemp1.right != null && NodeTemp2.left != null){
                                NodeTemp1.right.next = NodeTemp2.left;
                                //System.out.println(NodeTemp1.right.val +" Next : "+ NodeTemp2.left.val);
                            }
                            
                            if(i == arrayTemp.size()-2){
                                if(NodeTemp2.left != null && NodeTemp2.right != null){
                                    NodeTemp2.left.next = NodeTemp2.right;
                                    //System.out.println(NodeTemp2.left.val +" Next : "+ NodeTemp2.right.val);
                                }
                            }
    
                        }
                        
                        for(int i=0;i<arrayTemp.size();i++) {
                            TreeLinkNode NodeTemp = (TreeLinkNode)arrayTemp.get(i);
                            if(NodeTemp.left != null ){
                                queListFlag.add(NodeTemp.left);
                            }
                            if(NodeTemp.right != null ){
                                queListFlag.add(NodeTemp.right);
                            } 
                        }
                    }
                }
                
            }
        }
    }
     思路三:不采用其它的存储空间
        题目要求:You may only use constant extra space.
        所以逐层遍历节点的时候,需要将下一层节点的都连接上
     
    抄袭高人的代码如下:
    public class Solution {
        public void connect(TreeLinkNode root) {
            TreeLinkNode levelHead = root, nextLevelHead = null;
            while (levelHead != null) {
                TreeLinkNode node = levelHead, tail = null;
                while (node != null) {
                    if (node.left != null && node.right != null) {
                        node.left.next = node.right;
                    }
                    TreeLinkNode sub;
                    if (node.left != null)
                        sub = node.left;
                    else if (node.right != null)
                        sub = node.right;
                    else
                        sub = null;
                    if (sub != null) {
                        if (nextLevelHead == null) {
                            nextLevelHead = sub;
                            tail = sub;
                        } else {
                            tail.next = sub;
                        }
                        while (tail.next != null)
                            tail = tail.next;
                    }
                    node = node.next;
                }
                levelHead = nextLevelHead;
                nextLevelHead = null;
            }
        }
    }
     
     
  • 相关阅读:
    《学习OpenCV》第一版课后习题解答
    【练习8.11】等级匹配cvMatchContourTrees、凸缺陷计算cvConvexityDefects
    支持与不支持in-place操作的OpenCV函数汇总
    【练习8.10】直接使用cvFindContour的结果图片和cvDrawContour的方式提取Hu矩,观察在图片缩放或旋转时的稳定性
    【练习8.7】cvGoodFeaturesToTrack确定图像强角点、cvFindCornerSubPix亚像素级角点检测
    【练习8.6】使用不同参数值观察cvFindDominantPoints寻找关键点的效果
    【练习8.5】轮廓长度计算机cvApproxPoly逼近
    【练习8.2】使用指定标志创建序列cvCreateSeq、在序列中插入元素
    【练习8.1】查找轮廓、寻找关键点cvFindDominantPoints、访问序列中的元素
    C或C++中struct内存对齐计算精简方案
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4812811.html
Copyright © 2011-2022 走看看