• 《Cracking the Coding Interview》——第4章:树和图——题目4


    2014-03-19 03:40

    题目:给定一棵二叉树,把每一层的节点串成一个链表,最终返回一个链表数组。

    解法:前序遍历,遍历的同时向各个链表里添加节点。水平遍历好像还不如前序遍历来得方便。

    代码:

      1 // 4.4 Level order traversal
      2 #include <cstdio>
      3 #include <vector>
      4 using namespace std;
      5 
      6 struct TreeNode {
      7     int val;
      8     TreeNode *left;
      9     TreeNode *right;
     10     
     11     TreeNode(int _val = 0): val(_val), left(nullptr), right(nullptr) {};
     12 };
     13 
     14 struct ListNode {
     15     int val;
     16     ListNode *next;
     17     
     18     ListNode(int _val = 0): val(_val), next(nullptr) {};
     19 };
     20 
     21 void consructBSTFromSortedArray(vector<int> &v, int left, int right, TreeNode *&root)
     22 {
     23     if (left > right) {
     24         root = nullptr;
     25     } else {
     26         int mid = (left + right + 1) / 2;
     27         root = new TreeNode(v[mid]);
     28         consructBSTFromSortedArray(v, left, mid - 1, root->left);
     29         consructBSTFromSortedArray(v, mid + 1, right, root->right);
     30     }
     31 }
     32 
     33 void preorderTraversal(TreeNode *root, vector<ListNode *> &listHeads, vector<ListNode *> &listTails, int depth)
     34 {
     35     if (root == nullptr) {
     36         printf("# ");
     37     } else {
     38         while ((int)listHeads.size() < depth) {
     39             listHeads.push_back(nullptr);
     40             listTails.push_back(nullptr);
     41         }
     42         
     43         if (listHeads[depth - 1] == nullptr) {
     44             listHeads[depth - 1] = listTails[depth - 1] = new ListNode(root->val);
     45         } else {
     46             listTails[depth - 1]->next = new ListNode(root->val);
     47             listTails[depth - 1] = listTails[depth - 1]->next;
     48         }
     49         
     50         printf("%d ", root->val);
     51         preorderTraversal(root->left, listHeads, listTails, depth + 1);
     52         preorderTraversal(root->right, listHeads, listTails, depth + 1);
     53     }
     54 }
     55 
     56 void clearBinaryTree(TreeNode *&root)
     57 {
     58     if (root == nullptr) {
     59         return;
     60     } else {
     61         clearBinaryTree(root->left);
     62         clearBinaryTree(root->right);
     63         delete root;
     64         root = nullptr;
     65     }
     66 }
     67 
     68 void clearList(ListNode *&root)
     69 {
     70     ListNode *ptr;
     71     
     72     ptr = root;
     73     while (ptr != nullptr) {
     74         root = root->next;
     75         delete ptr;
     76         ptr = root;
     77     }
     78     root = nullptr;
     79 }
     80 
     81 int main()
     82 {
     83     TreeNode *root;
     84     int i, n;
     85     vector<int> v;
     86     vector<ListNode *> listHeads, listTails;
     87     ListNode *ptr;
     88     
     89     while (scanf("%d", &n) == 1 && n > 0) {
     90         for (i = 0; i < n; ++i) {
     91             v.push_back(i + 1);
     92         }
     93         
     94         consructBSTFromSortedArray(v, 0, n - 1, root);
     95         preorderTraversal(root, listHeads, listTails, 1);
     96         printf("
    ");
     97         
     98         for (i = 0; i < (int)listHeads.size(); ++i) {
     99             printf("Level %d:", i + 1);
    100             ptr = listHeads[i];
    101             while (ptr != nullptr) {
    102                 printf(" %d", ptr->val);
    103                 ptr = ptr->next;
    104             }
    105             printf("
    ");
    106             clearList(listHeads[i]);
    107         }
    108         
    109         v.clear();
    110         clearBinaryTree(root);
    111         listHeads.clear();
    112         listTails.clear();
    113     }
    114     
    115     return 0;
    116 }
  • 相关阅读:
    Effective Java 19 Use interfaces only to define types
    Effective Java 18 Prefer interfaces to abstract classes
    Effective Java 17 Design and document for inheritance or else prohibit it
    Effective Java 16 Favor composition over inheritance
    Effective Java 15 Minimize mutability
    Effective Java 14 In public classes, use accessor methods, not public fields
    Effective Java 13 Minimize the accessibility of classes and members
    Effective Java 12 Consider implementing Comparable
    sencha touch SortableList 的使用
    sencha touch dataview 中添加 button 等复杂布局并添加监听事件
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3610473.html
Copyright © 2020-2023  润新知