LeetCode—***Populating Next Right Pointers in Each Node寻找树的层次上的右指针

来源:互联网 发布:牙齿填骨粉的利弊 知乎 编辑:程序博客网 时间:2024/06/11 01:39

Populating Next Right Pointers in Each Node

 Total Accepted: 22481 Total Submissions: 63654My Submissions

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 -> NULLAfter calling your function, the tree should look like:

这道题目非常有意思,就是根据二叉树寻找自己水平向右的节点,如果没有那么就为null。

下面是自己的解题思路,但是程序还有点问题

首先是将数进行层序遍历,存入一个vector当中,然后利用一个特点,每一层的最后一个索引为2的指数倍-1,利用这一点进行相应的判断


/** * 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 {    queue<TreeLinkNode *> TreeNodeQueue;    vector<TreeLinkNode *> TreeNodeVector;public:    void connect(TreeLinkNode *root) {        if(root == NULL || root->left == NULL)        {            return;        }        TreeNodeQueue.push(root);        Scan(root);        connectVectorTree();    }    void Scan(TreeLinkNode * Node)    {        if(Node == NULL)        {            return;        }        if(Node->left)        {            TreeNodeQueue.push(Node->left);        }        if(Node->right)        {            TreeNodeQueue.push(Node->right);        }        TreeNodeVector.push_back(Node);        TreeNodeQueue.pop();        Scan(TreeNodeQueue.front());    }    void connectVectorTree()    {        int count = 1;        int CuurentCount = 2;        vector<TreeLinkNode *>::size_type size = TreeNodeVector.size();        for(int i = 0; i < (size-1); i++)        {            if(count < (CuurentCount-1))            {                TreeNodeVector[i]->next = TreeNodeVector[i+1];                count++;            }            else            {                count++;                CuurentCount *= 2;            }        }    }};

后面看了网上的解题思路,发现自己犯了一个很严重的错误就是,忽略了next指针这个本身就有的特性,next指针将链表的含义引入了树结构中,因此解题思路可以利用链表的一些特点,这一个方法对于不是完全二叉树依旧是成立的
<pre name="code" class="cpp">/** * 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 {    queue<TreeLinkNode *> TreeNodeQueue;    vector<TreeLinkNode *> TreeNodeVector;public:    void connect(TreeLinkNode *root) {        if(root == NULL)        {            return;        }        TreeLinkNode * NextNode = root->next;        while(NextNode)        {            if(NextNode->left)            {                NextNode = NextNode->left;                break;            }            if(NextNode->right)            {                NextNode = NextNode->right;                break;            }            NextNode = NextNode->next; //<找到水平方向最靠右的一个节点,然后找到下一层次可以指向的位置        }        if(root->left)        {            root->left->next = root->right? root->right : NextNode; //<这里还有一个小问题就是如果右边的节点为空那么应该指向NextCode需要注意        }        if(root->right)        {            root->right->next = NextNode;        }        connect(root->right);        connect(root->left);    }        };




0 0