Leetcode: Populating Next Right Pointers in Each Node

来源:互联网 发布:淘宝借呗客服电话 编辑:程序博客网 时间:2024/05/22 13:21

题目:
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

思路分析:
我们首先利用队列queue层次遍历二叉树将结果放入vector中,因为题意假设是完全二叉树,所以二叉树的每层都是2i个节点。利用这个规律我们可以得到每一层的节点,然后循环的从每一层第一个节点指向右边后一个节点。

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) {} * }; */class Solution{public:    void connect(TreeLinkNode *root)    {        if (!root) return;        queue<TreeLinkNode*> nodeQueue;        vector<TreeLinkNode*> nodeVector;//二叉树层次遍历结果        nodeQueue.push(root);        TreeLinkNode *treeNode = nullptr;        //二叉树层次遍历        while (!nodeQueue.empty())        {            treeNode = nodeQueue.front();            nodeQueue.pop();            nodeVector.push_back(treeNode);            if (treeNode->left) nodeQueue.push(treeNode->left);            if (treeNode->right) nodeQueue.push(treeNode->right);        }        vector<TreeLinkNode*>::size_type size = nodeVector.size();        int count = 0;//遍历过程中节点指针        int level = 0;//树的层        int step = 0;//记录满二叉树节点的个数        while (count < size)        {            step += int(pow(2, level));            while ((count < step - 1) && (count < size))            {                nodeVector[count]->next = nodeVector[++count];            }            count++;            level++;        }    }};

Java参考代码:

/** * 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> nodeQueue = new LinkedList<TreeLinkNode>();        List<TreeLinkNode> nodeList = new ArrayList<TreeLinkNode>();        TreeLinkNode treeNode = null;        nodeQueue.offer(root);        //层次遍历二叉树,结果放入nodeList中        while (!nodeQueue.isEmpty()) {            treeNode = nodeQueue.poll();            nodeList.add(treeNode);            if (treeNode.left != null) nodeQueue.offer(treeNode.left);            if (treeNode.right != null) nodeQueue.offer(treeNode.right);        }        int size = nodeList.size();        int count = 0;        int level = 0;        int step = 0;        while (count < size) {            step += Math.pow(2, level);            while ((count < step - 1) && (count < size)) {                nodeList.get(count).next = nodeList.get(++count);            }            ++count;            ++level;        }    }}
0 0
原创粉丝点击