Leetcode:108.将有序数组转换为二叉搜索树&&Leetcode:109.将有序链表转换成二叉搜索树

133 篇文章 0 订阅

Leetcode:108.将有序数组转换为二叉搜索树

将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

示例:

给定有序数组: [-10,-3,0,5,9],

一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:

      0
     / \
   -3   9
   /   /
 -10  5

解题思路:

1. 计算数组规模size = nums.size()。生成规模为size的平衡二叉搜索树,从高往低依次分配结点,直到规模等于size则停止分配。

2. 按照中序遍历的顺序,将数组的值依次写入二叉树中。

                                         

C++代码
#define hasLChild(x) (!(x->left==NULL))
#define hasRChild(x) (!(x->right==NULL))
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        int size = nums.size();
        if (size == 0) return NULL;
        //生成一个节点个数为size的平衡二叉树。
        TreeNode* root = Balanced_binary_tree(size);
        //中序遍历顺序,将数组写入平衡二叉树。
        In_order(root, nums);
        return root;
    }
    TreeNode* Balanced_binary_tree(int size) {
        queue<TreeNode*> Q;
        TreeNode* temp = new TreeNode(0);
        TreeNode* root = temp;
        Q.push(temp);
        size--;
        while (size > 0) {
            temp = Q.front();
            temp->left = new TreeNode(0);
            Q.push(temp->left);
            size--;
            if (size > 0) {
                temp->right = new TreeNode(0);
                Q.push(temp->right);
            }
            size--;
            Q.pop();
        }
        return root;
    }
    void In_order(TreeNode* root, vector<int>& nums) {
        if (hasLChild(root)) In_order(root->left, nums);
        root->val = nums[pos++];
        if (hasRChild(root)) In_order(root->right, nums);
    }
private:
    int pos = 0;
};

Leetcode:109.将有序链表转换成二叉搜索树

给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

示例:

给定的有序链表: [-10, -3, 0, 5, 9],

一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:

      0
     / \
   -3   9
   /   /
 -10  5

解题思路:

将链表转换成数组,然后调用108题的方法即可,当然,也可以修改In_order函数,改为链表的中序遍历,其实差距不大。

                                              

C++代码
#define hasLChild(x) (!(x->left==NULL))
#define hasRChild(x) (!(x->right==NULL))
class Solution108 {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        int size = nums.size();
        if (size == 0) return NULL;
        //生成一个节点个数为size的平衡二叉树。
        TreeNode* root = Balanced_binary_tree(size);
        //中序遍历顺序,将数组写入平衡二叉树。
        In_order(root, nums);
        return root;
    }
    TreeNode* Balanced_binary_tree(int size) {
        queue<TreeNode*> Q;
        TreeNode* temp = new TreeNode(0);
        TreeNode* root = temp;
        Q.push(temp);
        size--;
        while (size > 0) {
            temp = Q.front();
            temp->left = new TreeNode(0);
            Q.push(temp->left);
            size--;
            if (size > 0) {
                temp->right = new TreeNode(0);
                Q.push(temp->right);
            }
            size--;
            Q.pop();
        }
        return root;
    }
    void In_order(TreeNode* root, vector<int>& nums) {
        if (hasLChild(root)) In_order(root->left, nums);
        root->val = nums[pos++];
        if (hasRChild(root)) In_order(root->right, nums);
    }
private:
    int pos = 0;
};
class Solution{
public:
    TreeNode* sortedListToBST(ListNode* head) {
        vector<int> nums;
        while (head != NULL) {  nums.push_back(head->val); head = head->next;}
        return Solution108().sortedArrayToBST(nums);
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值