LeetCode116. Populating Next Right Pointers in Each Node 填充每个节点的下一个右侧节点指针(Java)

题目:

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *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.

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.
    LeetCode116. Populating Next Right Pointers in Each Node 填充每个节点的下一个右侧节点指针(Java)_第1张图片

Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

解答:

本题一开始理解了很久,注意题目的意思是:
给出一棵完美二叉树,即每个非叶子结点都有两个子节点,原本每个节点的 next 都指向 null。现要求将树的每层用next指针连接起来,并且每层的最后一个最右节点指向null

不使用额外空间的话,采用原地层级遍历,递归实现,思路如下:

  1. 若当前节点为空,或者左子树为空(即为叶子节点),则递归结束
  2. 将节点的左子树指向节点的右子树,即 root.left.next = root.right
  3. 注意如果 root.next != null,则说明当前节点为左子树,则应该将该节点的右子树指向该节点右兄弟的左子树
  4. 继续递归
/*
// 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) {
        while(root == null || root.left == null) {
            return root;
        }
        root.left.next = root.right;
        if(root.next != null) {
            root.right.next = root.next.left;
        }
        connect(root.left);
        connect(root.right);
        return root;
    }
}

如果不要求空间复杂度,也可以通过队列来实现。但明显下面方法没有上述方法好,提供一种思路而已

/*
// 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) {
        while(root == null) {
            return root;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            Node pre = null;
            for(int i=0; i<size; i++) {
                Node cur = queue.poll();
                //跳过第一个节点
                if(i > 0) {
                    pre.next = cur;
                }
                pre = cur;
                if(cur.left != null) {
                    queue.add(cur.left);
                }
                if(cur.right != null) {
                    queue.add(cur.right);
                }
            }
        }
        return root;
    }
}

你可能感兴趣的:(LeetCode)