【leetcode】每日精选题详解之116. 填充每一个节点的下一个右侧节点指针

        嗨,你们好,我是袁小厨(由于酷爱作饭,因此本身考取了厨师证)。以前一直看你们写的博客,学到了不少东西。而后最近萌生了本身写的想法,将本身知道的分享给须要的同窗。之后天天会为你们分享leetcode精选题目的各类题解和Python, JS, JQ, CSS, PHP, JAVA的一些小Demo。请你们关注我,一块儿交流学习吧。

java


题目描述

在这里插入图片描述


递归解法

作题思路

这道题的含义意思就是将每层的节点用链表给链接起来,由于这个题目是完美二叉树,因此咱们能够利用递归,而后还能够利用层次遍历。递归算法须要注意的地方就是,咱们不能忽略了2节点的右孩子和3节点的左孩子这一条线。因此咱们须要新建一个函数来进行递归。

node

题目代码

class Solution { 
    public Node connect(Node root) { 
        if(root==null){ 
            return null;
        }
        //将左右孩子传入函数中
        connectTwoNode(root.left,root.right);
        return root;

        
    }
    public void connectTwoNode(Node node1, Node node2){ 
           if(node1==null||node2==null){ 
               return ;
           }
           node1.next = node2;
           //三种须要链接的状况
           connectTwoNode(node1.left,node1.right);
           connectTwoNode(node2.left,node2.right);
           connectTwoNode(node1.right,node2.left);
    }

}

        


层次遍历(队列)

作题思路

关于层次遍历的思想你们能够参考这个文章二叉树的层次遍历。而后咱们能够直接用层次遍从来作这个题及填充每一个节点的右指针2。须要注意的事咱们刚才说的递归法不适用作第二题。由于每一个节点的孩子状况可能不同。不是完美二叉树。

算法

题目代码

class Solution { 
    public Node connect(Node root) { 
        //定义一个队列,用于层次遍历
        Queue<Node> queue = new LinkedList<Node>();
        if(root == null){ 
            return root;
        } 
        queue.offer(root);
        while(!queue.isEmpty()){ 
            int size = queue.size();
            //建立一个指针,用于链接该层,注意的是,这个指针的摆放位置,每一层更新一次
            Node temp = new Node(0);
            for(int i = 0 ; i < size ; i++){ 
                Node q = queue.poll();//出列
                temp.next = q;//指针指向该节点
                temp = temp.next;//移动指针
                Node left = q.left;
                Node right = q.right;
                if(left!=null){ 
                    queue.offer(left);
                }
                if(right!=null){ 
                    queue.offer(right);
                }
            }

        }
        return root;        
    }
}

BFS

作题思路

这个算法算是对刚才算法的改进,速度更快,内存更小。主要思想就是设置一个哑结点temp,游离在树以外,temp.next指向该层的头结点。而后一层一层的添加,用一个cur节点进行,定位,而后将其子节点链接起来,而后cur进行移动,再链接其子孩子,而后cur跳入下一层。pre的功能就是用来链接cur的子节点。该题思想和数组的双重循环相似。这个题目的思想是我跟题解里面的一个大神学习的,图也来自与大神。你们能够也能够本身去看一下。BFS题解
在这里插入图片描述
在这里插入图片描述

数组

在这里插入图片描述
在这里插入图片描述

题目代码

public Node connect(Node root) { 
        if (root == null)
            return root;
         //用来定位层
        Node cur = root;
        while (cur != null) { 
           //哑结点,游离在树以外,用来预约位层数
            Node dummy = new Node(0);
            //用来cur的子节点链接起来
            Node pre = dummy;
            //而后开始遍历当前层的链表
            while (cur != null) { 
               //左子树链接
                if (cur.left != null) { 
                    pre.next = cur.left;              
                    pre = pre.next;
                }
                //右子树链接
                if (cur.right != null) { 
                    pre.next = cur.right;
                    pre = pre.next;
                }
                //到该层的下一个节点
                cur = cur.next;
            }   
            //到下一层 
            cur = dummy.next;
        }
        return root;
    }

总结

该题作法挺多,学会这几种方法,能够对作其余题有很大帮助。目前作树的题目能够有本身的想法。但愿和你们共同进步,对你们有帮助的话就点赞关注吧,天天都会更新哦。

做者:LeetCode
连接:https://leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode/
来源:力扣(LeetCode)
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。


函数

相关文章
相关标签/搜索