将一个已排序的链表或数组转化成一棵平衡二叉树

Problem:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

解题思路:这里要充分利用元素的有序性,来构造一棵平衡二叉树,这样可以避免为了维持二叉树的平衡性,而进行各种旋转操作。可以每次都向树中插入一个序列中的中间元素,这样就可以保证该结点的左子树和右子树含有结点的数目相等(或只相差一个)。这样反复操作,就可以构造一颗类完全二叉树,肯定满足平衡二叉树的条件。

代码实现如下:

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        if(head == NULL)
            return NULL;
        ListNode* end;//尾结点
        TreeNode* root;//平衡二叉树根结点
        end = head;
        while(end->next)
            end = end->next;
        root = CreateAVLTree(head, end);
        
        return root;
    }
    
    TreeNode* CreateAVLTree(ListNode* head, ListNode* end)
    {
        TreeNode *root;//构造树结点
        ListNode *slow, *fast;//用于查找中间结点
        ListNode *newEnd;//构建新的尾结点
        newEnd = slow = fast = head;
        
        if(head == end)//链表只有一个结点
        {
            root = new TreeNode(head->val);
            return root;
        }
        while(fast != end && fast->next != end)//查找链表的中间结点
        {
            newEnd = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        
        root = new TreeNode(slow->val);
        if(newEnd != slow)
            root->left = CreateAVLTree(head, newEnd);
        root->right = CreateAVLTree(slow->next, end);
        
        return root;
    }
};

Problem:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

思路和上面完全一样,代码实现如下所示:

class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        TreeNode *root = NULL;
        if(!num.size())
            return NULL;
        int head, end;//处理数组元素的指针
        head = 0;
        end = num.size() - 1;
        root = CreateAVLTree(num, head, end);
        
        return root;
    }
    
    TreeNode *CreateAVLTree(vector<int> &num, int head, int end)
    {
        TreeNode *root;
        int middle = (head + end)/2;
        if(head == end)//只有一个元素
        {
            root = new TreeNode(num[head]);
            return root;
        }
        root = new TreeNode(num[middle]);
        if(middle != head)//容器内只有两个元素
            root->left = CreateAVLTree(num, head, middle-1);
        root->right = CreateAVLTree(num, middle+1, end);
        
        return root;
    }
};



好的,我来回答你的问题。 将一个二叉链表存储结构转换成一棵二叉树的方法是: 1. 从二叉链表中找到根节点,可以通过遍历整个二叉链表找到没有父节点的节点,即为根节点。 2. 对于每个非叶子节点,将其左子节点作为其左子树的根节点,将其右子节点作为其右子树的根节点。 3. 对于每个节点,将其在二叉链表中的父节点作为其在二叉树中的父节点。 接下来是二叉树的前序、中序和后序遍历序列的输出方法: 1. 前序遍历:先输出当前节点的值,然后递归遍历左子树,最后递归遍历右子树。 2. 中序遍历:先递归遍历左子树,然后输出当前节点的值,最后递归遍历右子树。 3. 后序遍历:先递归遍历左子树,然后递归遍历右子树,最后输出当前节点的值。 下面是示例代码,假设二叉链表存储结构已经存在,且有一个指向根节点的指针root: ``` // 前序遍历 void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } // 中序遍历 void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } // 后序遍历 void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } // 将二叉链表转换成二叉树 TreeNode* convertListToTree(ListNode* head) { // TODO: 实现二叉链表转换成二叉树的方法 } int main() { // 假设有一个指向二叉链表头结点的指针head TreeNode* root = convertListToTree(head); cout << "前序遍历:"; preOrder(root); cout << endl; cout << "中序遍历:"; inOrder(root); cout << endl; cout << "后序遍历:"; postOrder(root); cout << endl; return 0; } ``` 希望能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值