每日刷题(3)

题目

Leetcode108.将有序数组转化为二叉搜索树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode *sort_binary(vector<int>&nums,int l,int r){
        if(l  > r) return NULL;
        TreeNode * root = new TreeNode;
        root->val = nums[(l + r)/2];
        root->left = sort_binary(nums,l,(l + r)/2 - 1);
        root->right = sort_binary(nums,(l + r)/2 + 1,r);
        return root;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
       TreeNode * root;
        root = sort_binary(nums,0,nums.size()-1);
        return root;
    }

};

区分
平衡二叉树(Balanced Binary Tree具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。
一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的结点。

Leetcode110.平衡二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int height(TreeNode *root){  //高度
        if(root == NULL) return 0;
        int a = height(root->left) ;
        int b = height(root->right);
        return max(a,b) + 1;
    }
    bool isBalanced(TreeNode* root) {
        if(root == NULL) return true;
        //需要满足三个条件1.根的左右子树高度差小于一   2.左子树是平衡二叉树   3.右子树是平衡二叉树
        if(abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right)){
            return true;
        }else{
            return false;
        }
    }
};

Leetcode111.二叉树最小深度

错误代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == NULL) return 0;
        int a = minDepth(root->left);
        int b = minDepth(root->right);
        return min(a,b) + 1;
    }
};

错误案例
[1,2]
在这里插入图片描述此题关键在于省题,题目中说最小深度是从根节点到最近叶子节点的最短路径上的节点数量,所以案例[1,2]最小深度是2而不是1,所以上述代码应该加上节点有单个孩子的判断(只有左子树或只有右子树)
正确代码

 /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        int a = minDepth(root->left);
        int b = minDepth(root->right);
        if((root->left == nullptr && root->right != nullptr)||(root->left != nullptr && root->right == nullptr)){
            return (a == 0)?(b+1):(a+1); 
        }else{
             return min(a,b) + 1;
        }
    }
};

对比题Leetcode104.二叉树最大深度

发布了37 篇原创文章 · 获赞 36 · 访问量 3590

猜你喜欢

转载自blog.csdn.net/qq_43799957/article/details/105169759