• Convert Sorted List to Binary Search Tree


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

    C++代码实现:

    #include<iostream>
    #include<new>
    #include<vector>
    #include<cmath>
    using namespace std;
    
    //Definition for singly-linked list.
    struct ListNode
    {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };
    //Definition for binary tree
    struct TreeNode
    {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    };
    
    class Solution
    {
    public:
        TreeNode *sortedListToBST(ListNode *head)
        {
            if(head==NULL)
                return NULL;
            TreeNode *root=NULL;
            if(head->next==NULL)
            {
                root=new TreeNode(head->val);
                return root;
            }
            ListNode *p=head;
            ListNode *q=head;
            ListNode *pre=head;
            while(q&&q->next)
            {
                q=q->next->next;
                pre=p;
                p=p->next;
            }
            pre->next=NULL;
            q=p->next;
            p->next=NULL;
            root=new TreeNode(p->val);
            root->left=sortedListToBST(head);
            root->right=sortedListToBST(q);
            return root;
        }
        int maxDepth(TreeNode *root)
        {
            if(root)
            {
                if(root->left==NULL&&root->right==NULL)
                    return 1;
                int leftDepth=maxDepth(root->left);
                int rightDepth=maxDepth(root->right);
                return leftDepth>= rightDepth ?(leftDepth+1):(rightDepth+1);
            }
            return 0;
        }
        void inorder(TreeNode *root)
        {
            if(root)
            {
                inorder(root->left);
                cout<<root->val<<" ";
                inorder(root->right);
            }
        }
        void createList(ListNode *&head)
        {
            ListNode *p=NULL;
            int i=0;
            int arr[10]= {9,8,5,4,4,3,3,3,2,1};
            for(i=0; i<10; i++)
            {
                if(head==NULL)
                {
                    head=new ListNode(arr[i]);
                    if(head==NULL)
                        return;
                }
                else
                {
                    p=new ListNode(arr[i]);
                    p->next=head;
                    head=p;
                }
            }
        }
    };
    
    int main()
    {
        Solution s;
        TreeNode *root=NULL;
        ListNode *L=NULL;
        s.createList(L);
        root=s.sortedListToBST(L);
        s.inorder(root);
        cout<<endl;
        cout<<s.maxDepth(root)<<endl;
    }

    运行结果:

  • 相关阅读:
    html iframe 滚动条
    Angular-Ant Desigin 开篇
    端口访问不了的原因
    swift 加载 本地html 和 网络路径
    xcode9.4 报错 error:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
    viewDidLoad, viewWillDisappear, viewWillAppear等区别及各自的加载顺序
    JavaScript设计模式之一Interface接口
    ES6原生Class
    react portals
    react-native-pushy 热更新
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4116567.html
Copyright © 2020-2023  润新知