【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)
时间: 2024-08-05 01:54:42

【LeetCode】Populating Next Right Pointers in Each Node的相关文章

【LeetCode】 Populating Next Right Pointers in Each Node 完全二叉树

题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每个节点增加一个next指针,指向树状结构排列时自己的右边节点,如果右边没有节点则置为null * * Definition for binary tree with next pointer

【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * LeetCode Populating Next Right Pointers in Each Node * 题目:为树的每一个节点添加一个next指针.指向树状结构排列时自己的右边节点,假设右边没有节点则置为null * * Definition for binary tree with next pointe

【leetcode】Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra s

【leetcode】Populating Next Right Pointers in Each Node I &amp; II(middle)

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, al

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

【leetcode刷题笔记】Populating Next Right Pointers in Each Node II

What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tree, 1 / 2 3 / \ 4 5 7 After calling your function, the tree should look li

LeetCode[Tree]: Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

Leetcode 树 Populating Next Right Pointers in Each Node

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node Total Accepted: 13657 Total Submissions: 39540 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }