LeetCode #8 (#88、#100、#101)

88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]c
//Solution
//总结:倒着判断即可,因为他已经给了数组的大小了,num1够大所以不用开新的数组
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    int i, i1, i2;
    i1 = m - 1;
    i2 = n - 1;
    i = nums1Size - 1;
    
    while (i1 >= 0 && i2 >= 0) {
        if (nums1[i1] > nums2[i2]) {
            nums1[i--] = nums1[i1--];
        } else {
            nums1[i--] = nums2[i2--];
        }
    }
    
    while (i2 >= 0) {
        nums1[i--] = nums2[i2--];
    }
}

100. Same Tree

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Example 1:

Input:     1         1
         / \       / \
        2   3     2   3

       [1,2,3],   [1,2,3]

Output: true

Example 2:

Input:     1         1c
         /           \
        2             2

       [1,2],     [1,null,2]

Output: false

Example 3:

Input:     1         1
         / \       / \
        2   1     1   2

       [1,2,1],   [1,1,2]c

Output: falsec
//Solution
//总结:分情况判断即可
/**
 * 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 && q)
        return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right) && (p->val == q->val)? true : false);
    else if(!p && !q)
        return true;
    else
        return false;
}

101. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

   1
  / \
 2   2
/ \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:c

   1
  / \
 2   2
  \   \
  3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

//Solution
//总结:判断函数与上一题类似
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool isSymmetricRecursively(struct TreeNode* Lnode ,struct TreeNode* Rnode ){

    if(Lnode==NULL && Rnode==NULL)
        return true;
    
    if(Lnode==NULL || Rnode==NULL)
        return false ;


    return ((Lnode->val==Rnode->val) && isSymmetricRecursively(Lnode->left,Rnode->right) && isSymmetricRecursively(Lnode->right,Rnode->left )) ;
}
bool isSymmetric(struct TreeNode* root){
    if(root==NULL)
    return true;
    
   return isSymmetricRecursively(root->left ,root->right);
}


发布了72 篇原创文章 · 获赞 10 · 访问量 5840

猜你喜欢

转载自blog.csdn.net/weixin_44198992/article/details/105385514
今日推荐