首页 > 代码库 > 【LeetCode】Populating Next Right Pointers in Each Node

【LeetCode】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


    思路:

     要为每个节点的 next 指针赋值,指向本层的右边节点,最右节点 next 指针为空。下面是两种解法。


    代码:

    C++

    解一:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
#include<queue>

class Solution {
public:
    void connect(TreeLinkNode *root) {
		/* 定义处理队列 */
		queue<TreeLinkNode*> q;
		if(root != NULL) q.push(root);

		while(!q.empty()){
			/* 临时队列,用于存储下一层节点 */
			queue<TreeLinkNode *> next;

			while(!q.empty())
			{
				TreeLinkNode* cur = q.front();
				q.pop();

				/* 修改 next 指针 */
				if(!q.empty())
				{
					TreeLinkNode* val = q.front();
					cur->next = val;
				}
				else cur->next = NULL;

				/* 添加下一层节点 */
				if(cur->left != NULL)
					next.push(cur->left);
				if(cur->right != NULL)
					next.push(cur->right);
			}

			/* 复制队列 */
			if(!next.empty())
				q = next;
		}
        
    }
};

     解二:

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {

        /*节点为空,直接返回*/
        if (root==NULL) return;

        /*如果父节点有左节点和右节点,那么左节点的下一个是父节点的右节点*/
        if (root->left && root->right)
            root->left->next = root->right;

        /*如果父节点有右节点和 next 指针不空,则右节点指向父节点 next 节点的左节点 */
        if (root->next && root->right)
            root->right->next = root->next->left;

        /*左右孩子做连接*/
        connect(root->left);
        connect(root->right);

    }
};

     Python:

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#         self.next = None

class Solution:
    # @param root, a tree node
    # @return nothing
    def connect(self, root):
        if root == None:
            return

        if root.left != None and root.right != None:
            root.left.next = root.right

        if root.right != None and root.next != None:
            root.right.next = root.next.left

        self.connect(root.left)
        self.connect(root.right)


【LeetCode】Populating Next Right Pointers in Each Node