zoukankan      html  css  js  c++  java
  • [LeetCode 109]

    问题

    给出一个元素以递增序列排序的单链表,将其转换为一棵高度平衡的二叉搜索树。

    初始思路

    二叉搜索树高度平衡,意味着左右子树的高度要平衡。根据二叉树左子树节点小于根节点,右子树节点大于根节点的性质:我们在待选节点中选择值为中位数的节点作为根节点,所有小于中位数的节点作为左子树,所有大于中位数的节点作为右子树,即可满足高度平衡的要求。由于题目给出的已经是排好序的单链表,我们只要每次选择中间的节点即可。然后通过递归处理左右子树,最终完成高度平衡二叉搜索树的构建。

    最终完成代码如下:

     1 class Solution {
     2         enum ChildType
     3         {
     4             LEFT,
     5             RIGHT
     6         };
     7     public:
     8         TreeNode *sortedListToBST(ListNode *head)
     9         {
    10             if(!head)
    11             {
    12                 return nullptr;
    13             }
    14             
    15                         
    16             ListNode* middle = FindMiddlePoint(head, nullptr);
    17             
    18             TreeNode* root = new TreeNode(middle->val);
    19             
    20             MakeSubTree(head, middle, root, LEFT);
    21             MakeSubTree(middle->next, nullptr, root, RIGHT);
    22             
    23             return root;
    24         }
    25         
    26         void MakeSubTree(ListNode* head, ListNode* end, TreeNode* parent, ChildType childType)
    27         {
    28             if(head == end)
    29             {
    30                 return;
    31             }
    32             
    33             ListNode* middle = FindMiddlePoint(head, end);
    34             
    35             TreeNode* treeNode = new TreeNode(middle->val);
    36             
    37             if(childType == LEFT)
    38             {
    39                 parent->left = treeNode;
    40             }
    41             else
    42             {
    43                 parent->right = treeNode;
    44             }
    45             
    46             MakeSubTree(head, middle, treeNode, LEFT);
    47             MakeSubTree(middle->next, end, treeNode, RIGHT);
    48         }
    49         
    50         ListNode* FindMiddlePoint(ListNode* head, ListNode* end)
    51         {
    52             int length = 0;
    53             ListNode* node = head;
    54             
    55             while(node != end)
    56             {
    57                 ++length;
    58                 node = node->next;
    59             }
    60             
    61             length /= 2;
    62 
    63             node = head;
    64             for(int i = 0; i < length; ++i)
    65             {
    66                 node = node->next;
    67             }
    68 
    69             
    70             return node;
    71         }
    72     };
    sortedListToBST

    Judge Small和Judge Large皆顺利通过。

  • 相关阅读:
    知者不言,言者不知:论华人工程师之领导力
    vscode: Visual Studio Code 常用快捷键
    工作10年的我却没拼过刚毕业的美国女孩,亚裔们到底输在哪儿?
    不要再学习框架了!
    托福100分什么水平
    (转)Eclipse中快速输入System.out.println()的快捷键
    人生不相见,动如参与商
    Amazon onsite behavior question
    Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore
    浅谈volatile与automicInteger
  • 原文地址:https://www.cnblogs.com/shawnhue/p/leetcode_109.html
Copyright © 2011-2022 走看看