首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode: Populating Next Right Pointers in Each Node

Leetcode: Populating Next Right Pointers in Each Node

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 15:30:19
2880
发布2019-01-22 15:30:19
举报

题目: 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;
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年04月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档