C#学习笔记 (。・∀・)ノ゙
leetcode:
C#:110题、105题。
C实现:108题。
数据结构——树(Tree)
C语言除法&取整
/ :除法,向下取整(就取整数部分,负数同理)(eg. 结果等于1.6的取1,结果等于-1.6的取-1)
%:取余数
C语言取整函数
#include<math.h>
y1 = floor(x); //向下取整
y2 = ceil(x); //向上取整
C语言实现
leetcode108题:将有序数组转化为高度平衡的二叉搜索树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* BuildTree(int* nums,int left,int right)
{
if(left>right)
{
return NULL;
}
int mid=(left+right)/2;
//C语言要注意分配内存
struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val=nums[mid];
root->left=BuildTree(nums,left,mid-1);
root->right=BuildTree(nums,mid+1,right);
return root;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
return BuildTree(nums,0,numsSize-1);
}