码迷,mamicode.com
首页 > 其他好文 > 详细

109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)

时间:2015-10-03 15:30:38      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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

struct ListNode {
   int val;
   ListNode *next;
   ListNode(int x) : val(x), next(NULL) {}
 };
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) return NULL;
        int size = 1;
        ListNode * listNode = head->next;       
        while(listNode)
        {
            size++;
            listNode = listNode->next;            
        }
        TreeNode* root = new TreeNode(0);
        binarySearch(head,size,root);
        return root;
    }
    void binarySearch(ListNode *head,int size, TreeNode* treeNode) //二分法,对于Array,需要一头一尾两个指针;对于List需要头指针及size
    {     
        if(size == 1)
        {
            treeNode->val = head->val;
            return;
        }
        ListNode * listNode = head;  
        int mid = size>>1;
        for(int i = 1 ; i<mid;i++)
        {
            listNode = listNode ->next;
        }
        treeNode->val = listNode->next->val;
        ListNode* head2 = listNode->next->next;
        listNode->next = NULL;
        treeNode->left = new TreeNode(0); //先申请号空间,再传递。否则在被调用函数中指针本身值的变化并不会影响调用者
        binarySearch(head,mid,treeNode->left);
        if(head2)
        {
            treeNode->right = new TreeNode(0);
            binarySearch(head2,size-mid-1,treeNode->right);
        }  
    }
};

 

也可以通过引用传递,这样就不需要先初始化。

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        if(!head) return NULL;
        int size = 1;
        ListNode * listNode = head->next;       
        while(listNode)
        {
            size++;
            listNode = listNode->next;            
        }
        TreeNode* root = new TreeNode(0);
        binarySearch(head,size,root);
        return root;
    }
    void binarySearch(ListNode *head,int size, TreeNode* &treeNode) //按引用传递参数
    {     
        if(size == 1)
        {
            treeNode=new TreeNode(head->val);
            return;
        }
        ListNode * listNode = head;  
        int mid = size>>1;
        for(int i = 1 ; i<mid;i++)
        {
            listNode = listNode ->next;
        }
        treeNode = new TreeNode (listNode->next->val); //在被调用处申请空间
        ListNode* head2 = listNode->next->next;
        listNode->next = NULL;       
        binarySearch(head,mid,treeNode->left); //引用的是0x0000的地址
        if(head2)
        {
            binarySearch(head2,size-mid-1,treeNode->right);
        }  
    }
};

注意,NULL是一个宏定义

#define NULL 0

即NULL = (void*) 0

 

109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4853355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!