码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Populating Next Right Pointers in Each Node

时间:2015-04-05 01:16:00      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:二叉树

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

Leetcode: Populating Next Right Pointers in Each Node

标签:二叉树

原文地址:http://blog.csdn.net/theonegis/article/details/44879845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!