1. 程式人生 > >[每日演算法] leetcode第24題 Swap Nodes in Pairs、第 111題 Minimum Depth of Binary Tree

[每日演算法] leetcode第24題 Swap Nodes in Pairs、第 111題 Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree

原題目描述

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.

題目大意

題的本意是求樹的最小深度,但其實就是要找出一條從根節點到葉子節點的最短路徑。

解題思路

可以採用分治的演算法,分為左右子樹,判斷左右子樹的高度大小,選取小的+1就為高度,然後對左右子樹葉依次這樣遞推下去,直至葉子節點。當然,求取最短路徑其實也可以用DFS,但其實實現跟分治是差不多的,因為也需要遍歷所有的從根節點到葉子節點的路徑,再做一個回溯。此處只給出分治演算法的程式碼。

C++實現程式碼

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL)
            return 0;
        if (root->left == NULL && root->right == NULL)
            return 1;
        else if (root->left == NULL)
            return 1+minDepth(root->right);
        else if (root->right == NULL)
            return 1+minDepth(root->left);
        else {
            int l = 1+minDepth(root->left);
            int r = 1+minDepth(root->right);
            if (l > r)
                return r;
            else
                return l;
        }
    }
};

24. Swap Nodes in Pairs

原題目描述

Given a linked list, swap every two adjacent nodes and return its head. Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

Note:

Your algorithm should use only constant extra space.
You may not modify the values in the list's nodes, 
only nodes itself may be changed.

題目大意

將連結串列中的每兩個元素(按順序)兩兩交換。

解題思路

方法一

最開始想到的是利用兩個指標,分別標記前後元素,每次將父節點和子節點的val交換,然後兩個指標再向後移動兩個,直至連結串列結束,程式碼如下:

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode* par = head, *son = head->next;
        while(par && son) {
            swap(par->val, son->val);
            par = son->next;
            if (par)
                son = par->next;
            else
                son = NULL;
        }
        return head;
    }
};

方法二,題目要求不能改變節點的值,只能改變節點的位置,即我們可以通過改變next的指向來改變節點位置,程式碼大概如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode* H = head->next;
        head->next = H->next;
        H->next = head;

        ListNode* h = H->next->next;
        ListNode* pre = H->next;
        while(h && h->next) {
            ListNode* p = h;
            ListNode* t = p->next;
            p->next = t->next;
            t->next = p;
            h = p->next;
            pre->next = t;
            pre = p;
        }
        return H;
    }
};