leetcode -- 、993、994

993. Cousins in Binary Tree

Problem Description

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and y are cousins.

Solution Method

層次遍歷

typedef struct queue
{
    struct TreeNode * node;
    int lev;
}Queue;

bool isCousins(struct TreeNode* root, int x, int y)
{
    Queue *Q = (Queue * ) malloc (sizeof(Queue) * 1024);
    int front = 0, rear = 0, x_lev, y_lev;  // x, y所在層次
    Q[front].node = root;   // 根節點入隊
    Q[front++].lev = 0;
    
    while(front != rear)
    {
        struct TreeNode *pNode = Q[rear].node;      // 當前節點出隊
        int pNode_lev = Q[rear++].lev;
        
        // x,y節點是不是親兄弟
        if (pNode->left != NULL && pNode->right != NULL 
            && ((pNode->left->val == x && pNode->right->val == y) 
            || (pNode->left->val == y && pNode->right->val == x)))
            return false;
        // 記錄x,y的層次
        if (pNode->val == x)
            x_lev = pNode_lev;
        if (pNode->val == y)
            y_lev = pNode_lev;
        
        // 當前節點的孩子入隊
        if (pNode->left != NULL)
        {
            Q[front].node = pNode->left;
            Q[front++].lev = pNode_lev + 1;
        }
        if (pNode->right != NULL)
        {
            Q[front].node = pNode->right;
            Q[front++].lev = pNode_lev + 1;
        }
    }
    // 判斷x,y的層次是否一樣
    if (x_lev != y_lev)
        return false;
    return true;
}

在這裏插入圖片描述

994.Rotting Oranges

Problem Description

In a given grid, each cell can have one of three values:

the value 0 representing an empty cell;
the value 1 representing a fresh orange;
the value 2 representing a rotten orange.
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead.

在這裏插入圖片描述
Example 2:

Input: [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.

Example 3:

Input: [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Note:

  1. 1 <= grid.length <= 10
  2. 1 <= grid[0].length <= 10
  3. grid[i][j] is only 0, 1, or 2.

Solution Method

BFS

typedef struct queue
{
    int x;
    int y;
    int lev;
}Queue;

int orangesRotting(int** grid, int gridSize, int* gridColSize)
{
    int front = 0, rear = 0;    // 隊列指針
    int x_near[4] = {-1, 1, 0, 0};  // 用來表示x,y鄰近的元素
    int y_near[4] = {0, 0, -1, 1};
    
    Queue * Q = (Queue *) malloc (sizeof(Queue) * gridSize * gridColSize[0]);
    // 壞橘子入隊
    for (int i = 0; i < gridSize; i ++)
    {
        for (int j = 0; j < gridColSize[0]; j ++)
        {
            if (grid[i][j] == 2)
            {
                Q[rear].x = i;
                Q[rear].y = j;
                Q[rear++].lev = 0;
            }
        }
    }
    int x, y, lev = 0;
    
    while (front != rear)
    {
        // 隊首元素出隊
        x = Q[front].x;
        y = Q[front].y;
        lev = Q[front++].lev;

        // 看周圍是否有新鮮橘子,如果有,則入隊
        for (int i = 0; i < 4; i ++)
        {
            int xx = x + x_near[i];
            int yy = y + y_near[i];
            if (xx < 0 || xx >= gridSize || yy < 0 || yy >= gridColSize[0] || grid[xx][yy] != 1)
                continue;
            grid[xx][yy] = 2;
            Q[rear].x = xx;
            Q[rear].y = yy;
            Q[rear++].lev = lev+1;
        }
    }

    // 最後判斷是否還有新鮮橘子
    for (int i = 0; i < gridSize; i ++)
    {
        for (int j = 0; j < gridColSize[i]; j ++)
        {
            if (grid[i][j] == 1)        // 還有新鮮橘子
                return -1;
        }
    }
    return lev;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章