【问题标题】:Populating Next Right Pointers in Each Node of a binary tree在二叉树的每个节点中填充下一个右指针
【发布时间】:2016-01-13 05:59:13
【问题描述】:

我正在尝试在 C 中做 this problem on Leetcode

给定以下二叉树,

     1
   /  \
  2    3
 / \    \
4   5    7

调用你的函数后,树应该是这样的:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \    \
4-> 5 -> 7 -> NULL

我正在做的是为树的级别顺序遍历创建一个队列 并将每个节点的下一个指针连接到每个节点的下一个队列节点 等级。为了分隔级别,我将 NULL 指针加入队列。

所以对于上面的例子:: 队列是 -> [1,#, 2,3,#,4,5,7,#] 其中 # 为 NULL ptr。

这是我的问题代码::

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  struct TreeLinkNode *left, *right, *next;
 * };
 *
 */
bool isEmpty(int start,int end){
    if(start > end)
        return true;
    return false;
}

void connect(struct TreeLinkNode *root) {
    if(!root || (!root->left && !root->right))
        return;
    int cap = 1000;
    struct TreeLinkNode** q = malloc(cap* sizeof(struct TreeLinkNode*));
    int start=0, end=-1, curLevel=1, nextLevel=0;
    // enqueue
    q[++end] = root;
    while(isEmpty(start, end) == false){
        //dequeue
        struct TreeLinkNode* temp = q[start++];
        curLevel--;
        if(isEmpty(start, end) == false && curLevel !=0)
            temp->next = q[start];
        if(temp->left){
            q[++end] = temp->left;
            nextLevel++;
        }
        if(temp->right){
            q[++end] = temp->right;
            nextLevel++;
        }
        if(curLevel ==0){
            curLevel = nextLevel;
            nextLevel =0;
        }
        if(start> cap-50 || end> cap-50)
            q = realloc(q, 2*cap*sizeof(struct TreeLinkNode *));
    }
    free(q);
}

代码显然适用于小型测试用例,但对于 Leetcode 上的大型测试用例,代码会产生运行时错误。 我不知道我做错了什么。 请帮忙。如果有人可以在 Leetcode 上运行此代码,我将不胜感激

【问题讨论】:

  • 根据您写的内容,您似乎过于频繁地将 NULL 指针排队:它应该在每个级别之后,但在您的代码中,它在每个分析的节点之后。
  • 你是对的。为了解决这个问题,我做了一些改动。但是代码仍然无法正常工作。
  • 是的,感谢您指出错误

标签: c tree queue tree-traversal


【解决方案1】:

当您分析上一关卡的最后一个节点时,关卡结束。由于q中的每一层都以NULL结尾,所以当temp为NULL时,表示已分析完整层,您可以通过插入NULL来结束下一层。

所以我对你的代码做了一些修改:

void connect(struct TreeLinkNode *root) {
    int cap = 1000;
    struct TreeLinkNode** q = malloc(cap* sizeof(struct TreeLinkNode*));
    int start=0, end=-1;
    // enqueue
    q[++end] = root;
    q[++end] = NULL; // End of first level
    for (;;) {
        //dequeue
        struct TreeLinkNode* temp = q[start++];
        if (temp == NULL) {
            if (q[start] == NULL) // Two consecutives NULL means end of tree
                break;
            q[++end] = NULL; // End of level
        } else {
            temp->next = q[start];
            if(temp->left)
                q[++end] = temp->left;
            if(temp->right)
                q[++end] = temp->right;
            if (end > cap-50) { // end is always greater or equal to start
                q = realloc(q, 2*cap*sizeof(struct TreeLinkNode *));
            }
        }
    }
    free(q);
}

我现在无法实际测试它,所以它可能无法直接工作,主要是为了这个想法。

【讨论】:

    【解决方案2】:

    我会在 javascript 中提供我的结果,但我会提供足够的说明以另一种语言重现此结果的步骤:

    1. 使用level order traversal (breadth-first search) 解决此问题。
    2. 检查提供的root是否为null,如果是,我们立即返回
    3. 创建一个queue,我们将使用它来存储我们的二叉树节点
    4. 将根推入队列
    5. 启动仅当队列中有项目时才会运行的 while 循环
    6. 获取队列长度并将其存储在变量中,例如queueLength
    7. 遍历队列长度 (queueLength),同时观察循环的当前索引;
    8. queue 中移动/删除index 0 上的项目并将其另存为currValue
    9. 先检查是否有左节点,如果有则推入队列
    10. 检查它是否也有节点值,如果有则将其推入队列
    11. 如果循环中的索引值不等于queueLength-1,则currValue.next 将等于queue[0],即队列中的第一项。
    12. 如果循环中索引的值等于queueLength-1,那么这是本层的最后一个节点,我们可以将它的下一个值设置为null
    13. while 循环将再次重复 step 6 - 12
    const connect = function (root) {
        let queue = [];
        
        if(root){
            queue.push(root);
        }
    
        while (queue.length) {
            // let curr = queue.shift(1);
            const queueLength = queue.length;
    
            for (let i = 0; i < queueLength; i++) {
                const currValue = queue.shift(0);
                if (currValue.left) {
                    queue.push(currValue.left);
                }
    
                if (currValue.right) {
                    queue.push(currValue.right);
                }
    
                if (i !== queueLength - 1) {
                    currValue.next = queue[0];
                }
    
                if (i === queueLength - 1) {
                    currValue.next = null;
                }
            }
        }
    
        return root;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 2022-12-14
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多