zoukankan      html  css  js  c++  java
  • LeetCode: Convert Sorted List to Binary Search Tree

    第一次想用懒汉方法把list弄成vector再照前一题的方法,结果被判Memory exceed了。。然后只好再写次

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 /**
    10  * Definition for binary tree
    11  * struct TreeNode {
    12  *     int val;
    13  *     TreeNode *left;
    14  *     TreeNode *right;
    15  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    16  * };
    17  */
    18 class Solution {
    19 public:
    20     TreeNode *dfs(ListNode* head, int len) {
    21          if (!len) return NULL;
    22          int tmplen = len/2;
    23          ListNode *tmpl = head;
    24          while (tmplen && tmpl) {
    25              tmpl = tmpl->next;
    26              tmplen--;
    27          }
    28          TreeNode *tmp = new TreeNode(tmpl->val);
    29          tmp->left = dfs(head, len/2);
    30          tmp->right = dfs(tmpl->next, (len-1)/2);
    31          return tmp;
    32     }
    33     TreeNode *sortedListToBST(ListNode *head) {
    34         // Start typing your C/C++ solution below
    35         // DO NOT write int main() function
    36         if (!head) return NULL;
    37         int len = 0;
    38         ListNode *tmp = head;
    39         while (tmp) {
    40             len++;
    41             tmp = tmp->next;
    42         }
    43         return dfs(head, len);
    44     }
    45 };

     后来写了一个用双指针的写法。Interview的话应该这个程序更好

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 /**
    10  * Definition for binary tree
    11  * struct TreeNode {
    12  *     int val;
    13  *     TreeNode *left;
    14  *     TreeNode *right;
    15  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    16  * };
    17  */
    18 class Solution {
    19 public:
    20     TreeNode *sortedListToBST(ListNode *head) {
    21         // IMPORTANT: Please reset any member data you declared, as
    22         // the same Solution instance will be reused for each test case.
    23         if (head == NULL) return NULL;
    24         ListNode *mid = head;
    25         ListNode *end = head;
    26         ListNode *pre = new ListNode(0);
    27         pre->next = head;
    28         while (end) {
    29             end = end->next;
    30             if (end == NULL) break;
    31             end = end->next;
    32             mid = mid->next;
    33             pre = pre->next;
    34         }
    35         TreeNode *res = new TreeNode(mid->val);
    36         end = mid->next;
    37         if (pre->next == head) return new TreeNode(head->val);
    38         else pre->next = NULL;
    39         res->left = sortedListToBST(head);
    40         res->right = sortedListToBST(end);
    41         return res;
    42     }
    43 };

     C#

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     public int val;
     5  *     public ListNode next;
     6  *     public ListNode(int x) { val = x; }
     7  * }
     8  */
     9 /**
    10  * Definition for a binary tree node.
    11  * public class TreeNode {
    12  *     public int val;
    13  *     public TreeNode left;
    14  *     public TreeNode right;
    15  *     public TreeNode(int x) { val = x; }
    16  * }
    17  */
    18 public class Solution {
    19     public TreeNode SortedListToBST(ListNode head) {
    20         if (head == null) return null;
    21         ListNode mid = head;
    22         ListNode end = head;
    23         ListNode pre = new ListNode(0);
    24         pre.next = head;
    25         while (end != null) {
    26             end = end.next;
    27             if (end == null) break;
    28             end = end.next;
    29             mid = mid.next;
    30             pre = pre.next;
    31         }
    32         TreeNode ans = new TreeNode(mid.val);
    33         end = mid.next;
    34         if (pre.next == head) return new TreeNode(head.val);
    35         else pre.next = null;
    36         ans.left = SortedListToBST(head);
    37         ans.right = SortedListToBST(end);
    38         return ans;
    39     }
    40 }
    View Code
  • 相关阅读:
    c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
    c# 扩展方法奇思妙用基础篇九:Expression 扩展
    c# 扩展方法奇思妙用高级篇一:改进 Scottgu 的 "In" 扩展
    c# 扩展方法奇思妙用高级篇三:Enumerable.Cast<T> 应用
    c# 扩展方法奇思妙用高级篇二:Aggregate 扩展及其改进
    c# 扩展方法奇思妙用基础篇四:Random 扩展
    c# 扩展方法奇思妙用基础篇七:IsBetween 通用扩展
    c# 扩展方法奇思妙用基础篇三:byte 常用扩展
    c# 扩展方法奇思妙用基础篇八:Distinct 扩展
    c# 扩展方法奇思妙用基础篇 六:WhereIf 扩展
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/2969492.html
Copyright © 2011-2022 走看看