leetcode -- 872、897

872. Leaf-Similar Trees

Problem Description

Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.
在這裏插入圖片描述
For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

Note:

  • Both of the given trees will have between 1 and 100 nodes.

Solution Method

dfs, 下面代碼未free。

void dfs(struct TreeNode *root, int * arr,  int * len)
{
    if (root->left == NULL && root->right == NULL)      // 如果是葉子結點
    {
        *(arr + *len) = root->val;
        (*len) ++;
        return;
    }
    if (root->left != NULL)
    {
        dfs(root->left, arr, len);
    }
    if (root->right != NULL)
    {
        dfs(root->right, arr, len);
    }
}

bool leafSimilar(struct TreeNode* root1, struct TreeNode* root2)
{
    if (root1 == NULL && root2 == NULL)
        return true;
    else if ((root1 == NULL && root2 != NULL) || (root1 != NULL && root2 == NULL))
        return false;
    
    int * leavesArr1 = (int *) malloc (sizeof(int) * 1024);
    int * leavesArr2 = (int *) malloc (sizeof(int) * 1024);

    int len1 = 0, len2 = 0;     // 分別表示兩個數組元素長度

    dfs(root1, leavesArr1,&len1);
    dfs(root2, leavesArr2,&len2);

    if (len1 != len2)
        return false;
    
    for (int i = 0; i < len1; i ++)
    {
        if (leavesArr1[i] != leavesArr2[i])
            return false;
    }
    return true;
}

在這裏插入圖片描述

897. Increasing Order Search Tree

Problem Description

Given a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.

Example 1:
Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
在這裏插入圖片描述
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
在這裏插入圖片描述

Constraints:

  • The number of nodes in the given tree will be between 1 and 100.
  • Each node will have a unique integer value from 0 to 1000.

Solution Method

一開始題目都沒理解。
“Given a binary search tree, rearrange the tree in in-order”,這個翻譯成:給你一個樹,請你 按中序遍歷 重新排列樹。原來中序遍歷這麼翻譯的。

第二,題目要求返回的是一棵樹,而不是一個數組。
AC代碼如下:

// 其實就是中序遍歷
void dfs(struct TreeNode * root, struct TreeNode * resArr, int * len)
{
    if (root == NULL)
        return ;
    if (root->left != NULL)
    {
        dfs (root->left, resArr, len);
    }
    *(resArr + *len) = *root;
    (*len) ++;
    if (root->right != NULL)
    {
        dfs (root->right, resArr, len);
    }
}

struct TreeNode* increasingBST(struct TreeNode* root)
{
    if (root == NULL)
        return NULL;
    struct TreeNode * resArr = (struct TreeNode *) malloc (sizeof(struct TreeNode) * 1024);
    memset (resArr, '\0', sizeof(struct TreeNode) * 1024);

    int len = 0;
    dfs(root, resArr, &len);
    // 將resArr裏面的結點組成一棵樹
    for (int i = 0; i < len-1 ; i ++)
    {
        resArr[i].right = &resArr[i+1];
        resArr[i].left = 0;
    }
    return resArr;
}

在這裏插入圖片描述

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