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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csm201314/article/details/83045385

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;
    }
};

猜你喜欢

转载自blog.csdn.net/csm201314/article/details/83045385