zoukankan      html  css  js  c++  java
  • Leetcode 116. 填充每个节点的下一个右侧节点指针

    Leetcode 116. 填充每个节点的下一个右侧节点指针

    数据结构定义:

    给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
    
    struct Node {
      int val;
      Node *left;
      Node *right;
      Node *next;
    }
    填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
    
    初始状态下,所有 next 指针都被设置为 NULL。  
    /*
    // Definition for a Node.
    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;
    
        public Node() {}
        
        public Node(int _val) {
            val = _val;
        }
    
        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    };
    */
    

    递归写法:

    class Solution {
        public Node connect(Node root) {
            if(root == null){
                return null;
            }
            if(root.left != null){
                root.left.next = root.right;
                root.right.next = Objects.isNull(root.next) ? null : root.next.left;
                connect(root.left);
                connect(root.right);
            }
            return root;
        }
    }
    

    双指针递归:

    class Solution {
        /*
        * 思路: node 表示向下的指针, leftMost 表示同层级的指针
        * 每一次node指针遍历 ,leftMost连接同层级的所有节点
        */
        public Node connect(Node root) {
            if(root == null){
                return null;
            }
            Node node = root;
            while(node != null){
                Node leftMost = node;
                while(leftMost != null){
                    if(leftMost.left != null){
                        leftMost.left.next = leftMost.right;
                        leftMost.right.next = leftMost.next == null ? null:leftMost.next.left;
                    }
                    leftMost = leftMost.next;
                }
                node = node.left;
            }
            return root;
        }
    }
    

    运用队列进行迭代

    class Solution {
        public Node connect(Node root) {
            if(root == null) {return null;}
            Queue<Node> queue  =new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                for(int i =0;i<size;i++){
                    Node node = queue.poll();
                    if(i< size -1){
                        node.next = queue.peek();
                    }
                    if(node.left != null){
                        queue.offer(node.left);
                        queue.offer(node.right);
                    }
                }
            }
            return root;
        }
    }
    
  • 相关阅读:
    代码书写的细节
    php中的正则函数主要有三个-正则匹配,正则替换
    2个比较经典的PHP加密解密函数分享
    淘宝运营中的6大致命误区,你犯过么?
    30分钟教你写出10分的淘宝标题
    超全的web开发工具和资源
    转化率不好?告诉你转化飙秘诀
    帮你店铺日销千单的20个淘宝小技巧
    不刷单,中小卖家如何提升店铺流量?
    使用Flexible实现手淘H5页面的终端适配
  • 原文地址:https://www.cnblogs.com/CodingXu-jie/p/13984130.html
Copyright © 2011-2022 走看看