摘要: 题目 深度优先搜索 1 class Solution { 2 public: 3 int minDepth(TreeNode* root) { 4 if(root == NULL) return 0; 5 if(root->left == NULL) return minDepth(root->ri 阅读全文
posted @ 2021-01-04 11:19 Uitachi 阅读(101) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 bool isBalanced(TreeNode* root) { 4 if(root == NULL) return true; 5 return abs(height(root->left)-height(root->right 阅读全文
posted @ 2021-01-02 17:41 Uitachi 阅读(82) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 TreeNode* sortedArrayToBST(vector<int>& nums) { 4 if(nums.size() == 0) return NULL; 5 return build_BST(nums,0,nums.s 阅读全文
posted @ 2021-01-02 17:18 Uitachi 阅读(67) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 vector<vector<int>> levelOrderBottom(TreeNode* root) { 4 auto levelOrder = vector<vector<int>> (); 5 if(root == NULL 阅读全文
posted @ 2021-01-02 16:34 Uitachi 阅读(68) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 int maxDepth(TreeNode* root) { 4 if(root == NULL) return 0; 5 //return maxDepth(root->left) > maxDepth(root->right) 阅读全文
posted @ 2021-01-02 15:49 Uitachi 阅读(67) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 4 bool isSymmetric(TreeNode* root) { 5 return check(root,root); 6 } 7 bool check(TreeNode* p,TreeNode* q) { 8 if(!p 阅读全文
posted @ 2021-01-02 15:05 Uitachi 阅读(75) 评论(0) 推荐(0)
摘要: 题目 1 class Solution { 2 public: 3 bool isSameTree(TreeNode* p, TreeNode* q) { 4 if(p == NULL && q == NULL){ 5 return true; 6 }else if(p == NULL || q = 阅读全文
posted @ 2021-01-02 11:05 Uitachi 阅读(69) 评论(0) 推荐(0)
摘要: 题目描述 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), 阅读全文
posted @ 2021-01-02 10:16 Uitachi 阅读(83) 评论(0) 推荐(0)
摘要: 问题 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 代码 贪心算法 核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃——对应下面第六行代码 1 class Solution { 2 public: 3 int maxSubArray( 阅读全文
posted @ 2020-09-05 17:28 Uitachi 阅读(114) 评论(0) 推荐(0)
摘要: 问题 水题 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 你可以假设数组中无重复元素。 代码 自己写麻烦了,时间复杂度不如标答快! 1 class Solution { 2 public: 3 int searchInser 阅读全文
posted @ 2020-08-29 17:50 Uitachi 阅读(169) 评论(0) 推荐(0)