Estructura de datos --- preguntas básicas de la entrevista de árbol binario

1. Pregunta 144 de LeetCode: recorrido por adelantado del árbol binario

Enlace: enlace .
Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 //因为你需要知道在开辟空间大小的时候,开辟多大才行,所以要算结点的个数
 int TreeSize(struct TreeNode* root)
 {
    
    
    if(root == NULL)
        return 0;
    else
        return 1+TreeSize(root->left) + TreeSize(root->right);
 }

//前序遍历
 void _preorderTraversal(struct TreeNode* root,int* array,int* pi)
 {
    
    
    if(root == NULL)
        return;
    array[(*pi)++] = root->val;
    _preorderTraversal(root->left,array,pi); //这里的pi是传过来的指针,可以直接的使用
    _preorderTraversal(root->right,array,pi);
 }

//这里值得注意的就是在传i的时候一定要传地址,因为只有传值才能做到在同一个i上++的效果
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    
    
    int size = TreeSize(root);
    int* array = (int*)malloc(sizeof(int)*size);
    int i = 0;
    _preorderTraversal(root,array,&i); //思考题目就明白他需要你把结点的值都放在一个数组里面
    *returnSize = size;
    return array;
}

Inserte la descripción de la imagen aquí

2. Pregunta 94 de LeetCode: recorrido en orden del árbol binario

Enlace: enlace .

Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

 int TreeSize(struct TreeNode* root)
 {
    
    
    if(root == NULL)
        return 0;
    else
        return 1+TreeSize(root->left)+TreeSize(root->right);
 }
 
//左子树 根 右子树
 void _inorderTraversal(struct TreeNode* root,int* array,int* pi)
 {
    
    
    if(root == NULL)
        return;
    _inorderTraversal(root->left,array,pi);
    array[(*pi)++] = root->val;
    _inorderTraversal(root->right,array,pi);
 }

int* inorderTraversal(struct TreeNode* root, int* returnSize){
    
    
    int size = TreeSize(root);
    int* array = (int*)malloc(sizeof(int)*size);
    int i = 0;
    _inorderTraversal(root,array,&i);
    *returnSize = size;
    return array;
}

Inserte la descripción de la imagen aquí

3. Pregunta de LeetCode 145: recorrido posterior al orden del árbol binario

Enlace: enlace .
Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

  int TreeSize(struct TreeNode* root)
 {
    
    
    if(root == NULL)
        return 0;
    else
        return 1+TreeSize(root->left)+TreeSize(root->right);
 }

  void _postorderTraversal(struct TreeNode* root,int* array,int* pi)
 {
    
    
    if(root == NULL)
        return;
    _postorderTraversal(root->left,array,pi);
    _postorderTraversal(root->right,array,pi);
    array[(*pi)++] = root->val;
 }

int* postorderTraversal(struct TreeNode* root, int* returnSize){
    
    
    int size = TreeSize(root);
    int* array = (int*)malloc(sizeof(int)*size);
    int i = 0;
    _postorderTraversal(root,array,&i);
    *returnSize = size;
    return array;
}

Inserte la descripción de la imagen aquí

4. LeetCode Question 965-Single Value Binary Tree

Enlace: enlace .
Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

//分解为当前树 和左子树 右子树的子问题然后进行递归
bool isUnivalTree(struct TreeNode* root){
    
    
    if(root == NULL)
        return true;

    //检查当前树
    if(root->left && root->val != root->left->val)
        return false;
    if(root->right && root->val != root->right->val)
        return false;
        
    return isUnivalTree(root->left) && isUnivalTree(root->right);
}

Inserte la descripción de la imagen aquí

5. Pregunta 104 de LeetCode: profundidad máxima del árbol binario

Enlace: enlace .
Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


int maxDepth(struct TreeNode* root){
    
    
    if(root == NULL)
        return 0;
        //为的就是消除代码冗余
    int leftDepth = maxDepth(root->left);
    int rightDepth = maxDepth(root->right);

//求出左右子树较大的哪一个
    return leftDepth > rightDepth? leftDepth+1 :rightDepth+1;

}

Inserte la descripción de la imagen aquí

6. Problema de LeetCode 226-Flip Binary Tree

Enlace: enlace .
Inserte la descripción de la imagen aquí
Hay dos formas de resolver este problema: ①Voltee los subárboles izquierdo y derecho, luego invierta los subárboles izquierdo y derecho del subárbol izquierdo y los subárboles izquierdo y derecho del subárbol derecho, que es
Inserte la descripción de la imagen aquí
equivalente a un subárbol de orden medio el recorrido

//接口要求返回的是树的根结点
struct TreeNode* invertTree(struct TreeNode* root){
    
    
    if(root == NULL)
    {
    
    
        return NULL;
    }
       else
    {
    
    
        struct TreeNode* tmp = root->left;
        root->left = root->right;
        root->right = tmp;

        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
}

②Es
equivalente a un recorrido de seguimiento, el subárbol izquierdo se recorre primero, el subárbol derecho se recorre y la raíz es el último.

struct TreeNode* invertTree(struct TreeNode* root){
    
    
    if(root == NULL)
    {
    
    
        return NULL;
    }
    else
    {
    
    
        struct TreeNode* right = root->right ;
        root->right = invertTree(root->left);
        //这里有一个覆盖值的问题,你把左边求出来直接链在了右边,那么右边原来的就没有了被覆盖了,你在转换就不对了
        root->left = invertTree(right);
        return root;
    }
}

Inserte la descripción de la imagen aquí

7. Pregunta 100 de LeetCode: el mismo árbol

Enlace: enlace .
Inserte la descripción de la imagen aquí

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

//把每一种情况都考虑到
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
    
    
    if(p == NULL && q == NULL)
        return true;
    
    //结构不同
    if(p != NULL && q == NULL)
        return false;
    if(p == NULL && q != NULL)
        return false;
    
    //走到这里的时候就可以确定此时p和q都是不为空的,再来判断他们的值是否相同
    if(p->val != q->val) //此时==并不能判断出来结果,不能说他们一开始给的两个结点相同就直接返回true
        return false;

    
    
    return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
 
}

Inserte la descripción de la imagen aquí

8. Pregunta de LeetCode 572-subárbol de otro árbol

Enlace: enlace Esta
Inserte la descripción de la imagen aquí
pregunta necesita utilizar la interfaz de la pregunta anterior, lo que facilitará la solución.
Comparemos cada subárbol en tys, si son iguales, satisfaga

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
  bool isSameTree(struct TreeNode* p, struct TreeNode* q);
 bool isSameTree(struct TreeNode* p, struct TreeNode* q){
    
    
    if(p == NULL && q == NULL)
        return true;
    
    //结构不同
    if(p != NULL && q == NULL)
        return false;
    if(p == NULL && q != NULL)
        return false;
    
    //走到这里的时候就可以确定此时p和q都是不为空的,再来判断他们的值是否相同
    if(p->val != q->val) //此时==并不能判断出来结果,不能说他们一开始给的两个结点相同就直接返回true
        return false;

    
    
    return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
 
}

//t是s完全相同的一部分(包括叶子)
//子树就是和你的一个子树相不相同
//让t和s中的每一颗子树都进行比较,如果有相同,则满足
bool isSubtree(struct TreeNode* s, struct TreeNode* t){
    
    
    if(s == NULL)
        return false;
    
    if(isSameTree(s,t))
        return true;
    
    //此时我需要让我的t去和我的s中的每一结点所在的子树都去比较
    return  isSubtree(s->left,t) || isSubtree(s->right,t);
}

Inserte la descripción de la imagen aquí

9. Dedo de espada, artículo de oferta 28: árbol binario simétrico

Enlace: enlace . Para
Inserte la descripción de la imagen aquí
determinar si un árbol es simétrico, primero debe determinar si los elementos secundarios izquierdo y derecho son simétricos e iguales. También debe determinar si el subárbol izquierdo del elemento secundario izquierdo es simétrico con el subárbol derecho del elemento secundario derecho , y si el subárbol derecho del hijo izquierdo es el mismo que el hijo izquierdo del hijo derecho. El subárbol es simétrico.


bool _isSymmetric(struct TreeNode* left, struct TreeNode* right)
{
    
    
    //这两种if情况判断的都是结构上面的不同
    if(left == NULL && right == NULL)
        return true;
    if(left == NULL || right == NULL)
        return false;
    return left->val == right->val
        && _isSymmetric(left->left, right->right)
        && _isSymmetric(left->right, right->left);
}
 
bool isSymmetric(struct TreeNode* root){
    
    
    if(root == NULL)
        return true;
    return _isSymmetric(root->left, root->right);
}

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/MEANSWER/article/details/112752782
Recomendado
Clasificación