[LeetCode] 116. Populating Next Right Pointers in Each Node

题:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/

题目

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.
Recursive approach is fine, implicit stack space does not count as extra space for this problem.
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
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

思路

题目大意

给定一个满二叉树,每一层上的结点tNode,若它的右边仍存在结点nNode,则该节点tNode.next = nNode。

解题思路

方法一 广度遍历

对树进行广度遍历。在队列的一层结点中,一个结点的后一个结点便是 结点的next;若当前结点为该层的最后一个结点,那么该结点的next为null。

方法二 利用next指针

从高层到低层解决。
若一层结点的 next已经生成,那么下一层的next可以直接生成。
starti为一层的第一个节点 ,若starti.left 不为空,cur = starti。

  1. 对于已生成next的某结点cur,若cur不为空 。其左子结点的next 指向其右子结点。
  2. 若cur 的next存在的话,cur 右子结点的next 为 starti.next.left.
  3. 将cur = cur.next ,跳转 1。
    1~3实现了starti下一层中从左到右结点next的建立。 starti = starti.left,starti跳转到下一层。

code

方法一 广度遍历

/**
 * 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> queue = new LinkedList<>();
        queue.offer(root);
        
        
        while(queue.size() != 0){
            int nums = queue.size();

            for(int i = 1;i<nums;i++){
                TreeLinkNode tNode = queue.poll();
                tNode.next = queue.peek();
                if(tNode.left != null)  queue.offer(tNode.left);
                if(tNode.right != null)     queue.offer(tNode.right);   
            }
            TreeLinkNode tNode = queue.poll();
            tNode.next = null;
            if(tNode.left != null)  queue.offer(tNode.left);
            if(tNode.right != null)     queue.offer(tNode.right);            
        }
    }
}

方法二 利用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;
        TreeLinkNode starti = root;
        TreeLinkNode cur;
        while(starti.left != null){
            cur = starti;
            while(cur != null){
                cur.left.next = cur.right;
                if(cur.next != null) cur.right.next = cur.next.left;
                cur = cur.next;
            }
            starti = starti.left;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013383813/article/details/82853276