LeetCode刷题日记(Day13)

Problem 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]
    
  • 解题思路
    由于 nums1 足够大,所以在不申请额外空间的情况下,我们可以把 nums1 和 nums2 经过排序后存储在 nums1 中。方法如下:

  1. 令 len = m + n,表示需要进行排序的元素个数。
  2. 将 nums1 和 nums2 的最后一个有效元素进行对比,并将大的元素赋值给 nums1[len-1],重复此操作直到 nums1 和 nums2 被遍历完。
  • 代码实现
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int len = m + n;
        while(m>  0 && n > 0){
        	if(nums1[m-1] > nums2[n-1]){
        		nums1[len-1] = nums1[m-1];
        		m--;
        	}
        	else{
        		nums1[len-1] = nums2[n-1];
        		n--;
        	}
        	len--;
        }
        while(m > 0){
        	nums1[len-1] = nums1[m-1];
        	m--;
        	len--;
        }
        while(n > 0){
        	nums1[len-1] = nums2[n-1];
        	n--;
        	len--;
        }
    }
};

Problem 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
    
  • 解题思路
    判断一棵树是不是对称的,可采用递归的方式,规则如下:

  1. 对于根节点 root,若 root->left 为空,且 root->right 也为空,则返回 true;
  2. 若 root->left 和 left->right 只有一个为空,则返回 false;
  3. 若 root->left 和 root->right 都不为空,且 root->left->val 和 root->right->val 不相等,则返回false;
  4. 其它情况则分别对 root->left 和 root->right 进行递归。
  • 代码实现
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == NULL)
        	return true;
        return helper(root->left, root->right);
    }
    bool helper(TreeNode * left, TreeNode* right){
    	if(left == NULL && right == NULL)
    		return true;
    	if(left == NULL || right == NULL)
    		return false;
    	if(left->val != right->val)
    		return false;
    	return (helper(left->left, right->right) && helper(left->right, right->left));
    }
};

Problem 104. Maximum Depth of Binary Tree

  • 题目描述
    Given a binary tree, find its maximum depth.

    The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

    Note: A leaf is a node with no children.

    Example:

    Given binary tree [3,9,20,null,null,15,7],

        3
       / \
      9  20
        /  \
       15   7
    

    return its depth = 3.

  • 解题思路
    求一棵树的最大高度,可采用递归的方式,规则如下:

  1. 如果节点root为空,则返回0;
  2. 如果节点root不为空,则递归求出root的左子树和右子树高度max,并返回max+1。
  • 代码实现
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == NULL)
        	return 0;
        return 1+max(maxDepth(root->left), maxDepth(root->right));
    }
};

Problem 108. Convert Sorted Array to Binary Search Tree

  • 题目描述
    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

    For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

    Example:

    Given the sorted array: [-10,-3,0,5,9],
    
    One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
    
          0
         / \
       -3   9
       /   /
     -10  5
    
  • 解题思路
    采用递归的方式,令left、right、mid分别表示nums左元素、右元素、中间元素的下标。由于nums是有序的,所以根节点可以是nums[mid],再依次对左子树和右子树进行递归。

  • 代码实现

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
    	int len = nums.size();
    	if(len == 0)
    		return NULL;
    	return helper(nums, 0, len-1);
    }
    TreeNode* helper(vector<int> & nums, int left, int right){
    	if(left > right)
    		return NULL;
    	int mid = (left + right)/2;
    	TreeNode * pNode = new TreeNode(nums[mid]);
    	pNode->left = helper(nums, left, mid-1);
    	pNode->right = helper(nums, mid+1, right);
    	return pNode;
    }
};

Problem 94. Binary Tree Inorder Traversal

  • 题目描述
    Given a binary tree, return the inorder traversal of its nodes’ values.

  • 解题思路
    实现二叉树的中序遍历,可以使用递归的方式和非递归的方式,代码如下:

  • 代码实现
    递归方式:

    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> res;
            helper(root, res);
            return res;
        }
        void helper(TreeNode * root, vector<int> & res){
        	if(root == NULL)
        		return;
        	if(root->left != NULL)
        		helper(root->left, res);
        	res.push_back(root->val);
        	if(root->right != NULL)
        		helper(root->right, res);
        }
    };
    

    非递归方式:

    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode* root) {
        	stack<TreeNode *> s;
        	vector<int> res;
        	TreeNode* cur = root;
        	while(cur != NULL || !s.empty()){
        		while(cur != NULL){
        			s.push(cur);
        			cur = cur->left;
        		}
        		cur = s.top();
        		s.pop();
        		res.push_back(cur->val);
        		cur = cur->right;
        	}
        	return res;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值