LeetCode(49) Populating Next Right Pointers in Each Node I II

Populating Next Right Pointers in Each Node I

題目描述

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,

完全二叉樹

After calling your function, the tree should look like:

完全二叉樹next指針

題目要求給出完全二叉樹每個結點的next指針所指向的值。

解題思路

本題給定了限定條件,每棵樹均爲完全二叉樹,對一個結點L而言其子樹的結點的next指針分兩種情況:

  • 左子結點:L的右子節點(L->right);
  • 右子節點:L的next結點的左子結點(L->next->left),如果L本身爲NULL,則該結點的next指針也指向NULL。

再對L的左右子結點遞歸進行處理,則可處理整棵樹。

/**
 * 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) return;
        TreeLinkNode* left = root->left;
        TreeLinkNode* right = root->right;
        if(left) left->next = right;
        if(right)
        {
            if(root->next)
                right->next = root->next->left;
            else
                right->next = NULL;
        }
        connect(root->left);
        connect(root->right);
    }
};

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

二叉樹

After calling your function, the tree should look like:

二叉樹next指針

與第一題不同的是,本題求的是普通二叉樹的next指針。(第一題是完全二叉樹

解題思路

從第二題和第一題的區別可以看出,第二題不能保證每個結點的存在,所以對於當前結點L

1 左子節點:

1.1 L->right存在,則next = L->right;
1.2 L->right不存在,next = L->next->left;(循環查找知道L->next != NULL位置,若L->next ->left不存在則爲L->next->right);

2 右子節點與1.2的情況相同;

/**
 * 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 findNext(TreeLinkNode* p, TreeLinkNode* node)
    {
        if( !node ) return;
        while(node)
        {
            if(node->left)
            {
                p->next = node->left;
                break;
            }
            else if(node->right)
            {
                p->next = node->right;
                break;
            }
            node = node->next;
        }
    }
    void connect(TreeLinkNode *root) {
        if(!root) return;

        TreeLinkNode* left = root->left;
        TreeLinkNode* right = root->right;

        if(left)
        {
            if(right)   left->next = right;
            else    findNext(left, root->next);
        }
        if(right)   findNext(right, root->next);

        connect(right);
        connect(left);
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章