Data structure---basic binary tree interview questions

1. LeetCode question 144-Preorder traversal of binary tree

Link: link .
Insert picture description here

/**
 * 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;
}

Insert picture description here

2. LeetCode Question 94—In-order traversal of binary tree

Link: link .

Insert picture description here

/**
 * 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;
}

Insert picture description here

3. LeetCode question 145-post-order traversal of binary tree

Link: link .
Insert picture description here

/**
 * 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;
}

Insert picture description here

4. LeetCode Question 965-Single Value Binary Tree

Link: link .
Insert picture description here

/**
 * 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);
}

Insert picture description here

5. LeetCode Question 104—Maximum Depth of Binary Tree

Link: link .
Insert picture description here

/**
 * 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;

}

Insert picture description here

6. LeetCode Problem 226-Flip Binary Tree

Link: link .
Insert picture description here
There are two ways to solve this problem: ①Flip the left and right subtrees, then reverse the left and right subtrees of the left subtree and the left and right subtrees of the right subtree, which is
Insert picture description here
equivalent to a middle-order traversal

//接口要求返回的是树的根结点
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;
    }
}

②It is
equivalent to follow-up traversal, the left subtree is traversed first, the right subtree is traversed, and the root is last.

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;
    }
}

Insert picture description here

7. LeetCode question 100-the same tree

Link: link .
Insert picture description here

/**
 * 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);
 
}

Insert picture description here

8. LeetCode question 572-subtree of another tree

Link: link . This
Insert picture description here
question needs to use the interface of the previous question, which will make the solution easier.
Let each subtree in t and s be compared, if they are the same, satisfy

/**
 * 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);
}

Insert picture description here

9. Sword Finger Offer Item 28-Symmetric Binary Tree

Link: link . To
Insert picture description here
determine whether a tree is symmetrical, you must first determine whether the left and right children are symmetrical and equal. You also need to determine whether the left subtree of the left child is symmetrical with the right subtree of the right child, and whether the right subtree of the left child is the same as the left child of the right child. The subtree is symmetrical.


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);
}

Insert picture description here

Guess you like

Origin blog.csdn.net/MEANSWER/article/details/112752782