分类 默认分类 下的文章

题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

思路:
这道题是判断两个二叉树是否相等,各个节点和分支是否相等。依然使用递归遍历法,终结的条件是两个节点都为空,两个节点不一样,然后循环遍历左右节点,如果有一个节点不一样,那么返回的值始终为false。算法复杂度:O(N)

代码一:

/**

  • 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:

bool isSameTree(TreeNode* p, TreeNode* q)
{
    if( p == NULL && q == NULL )
    {
        return true;
    }
    else if( p == NULL && q != NULL )
    {
        return false;
    }
    else if( p != NULL && q == NULL )
    {
        return false;
    }
     
    bool bResult = true;
    if( p->val == q->val )
    {
        bResult = true;
    }
    else
    {
        bResult = false;
    }
     
    bool bSameLeft = isSameTree(p->left, q->left);
    bool bSameRight = isSameTree(p->right, q->right);
     
    if( !bSameLeft )
    {
        bResult = false;
    }
    if( !bSameRight )
    {
        bResult = false;
    }
    return bResult;
}

};
代码二:

/**

  • 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:

bool isSameTree(TreeNode* p, TreeNode* q)
{
    if( (p == NULL && q != NULL) || (p != NULL && q == NULL ) )
    {
        return false;
    }
     
    if( p == NULL && q == NULL )
    {
        return true;
    }

    if( p->val != q->val )
    {
        return false;
    }
     
    return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}   

};
总结:
1、二叉树的遍历:左节点递归遍历,右节点递归遍历。算法复杂度都是O(N)
2、两次提交成功,可以第一次是因为少了个";"号错误。
3、算法可以再优化,代码同样可以再优化。代码简洁、效率高、可读性强,这样才是一段好代码。代码二比一精简了一倍,但其可读性却更好了!

题目:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

思路:
该题目是求二叉树的最大深度,分析每个节点的规律可发现明显的规律,每个节点都有左右之分,依次去求左右节点的深度即可求其最大深度。使用递归法求解。复杂度为:O(n)

代码:

/**

  • 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 maxDepth(TreeNode* root) {
    if( root == NULL )
    {
        return 0;
    }

    int leftDepth = maxDepth(root->left);
    int rightDepth = maxDepth(root->right);
     
    return max(leftDepth, rightDepth) + 1;
}

};
总结:
1、对于有规则的循环嵌套可以使用迭代法
2、递归法要有一个终结点,找到适当的终结点即可。如该题是 root == NULL 终结掉

题目:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

思路:
建立一个map去存储已经循环过的数字,每次循环前先判断该值是否存在于map中,如果map内没有则添加进去,如果存在则清空该记录,最后循环完后,map里只有一条记录为最终该结果。算法复杂度:O(n)

代码:

class Solution {
public:

int singleNumber(vector<int>& nums) {
    map<int, int> vaule_map;
    for( unsigned int i = 0; i < nums.size(); i++ )
    {
        map<int, int>::iterator iIter = vaule_map.find(nums[i]);
        if( iIter == vaule_map.end() )
        {
            //empty
            vaule_map[nums[i]] = 1;
        }
        else
        {
            //find it and erase it
            vaule_map.erase(iIter);
        }
    }
    if( vaule_map.size() != 1 )
    {
        return -1;
    }
     
    map<int, int>::iterator iBegin = vaule_map.begin();
     
    return iBegin->first;
}

};
总结:
1、在网上看到其他解题思路,虽然可以节省内存,但着实没看下去。。
2、第一次提交一次通过!好开心。

题目:
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

思路:
刚开始没怎么看懂题目,明明是删除一个节点需要的输入参数是链表和节点,但题目给出参数只有节点,后来查了资料,才明白有一种思路叫做“狸猫换太子”,输入一个节点,把这个节点的下一个值赋值到这个节点,并删除下一个节点就OK了,而不是找到上一个节点的指向。算法复杂度:O(1)

代码:

/**

  • Definition for singly-linked list.
  • struct ListNode {
  • int val;
  • ListNode *next;
  • ListNode(int x) : val(x), next(NULL) {}
  • };
    */

class Solution {
public:

void deleteNode(ListNode* node) {
    if( node == NULL || node->next == NULL )
    {
        return;
    }
     
    ListNode *pNextNode = node->next;
    node->val = pNextNode->val;
    node->next = pNextNode->next;
    delete pNextNode;
}

};

赛事
2015年9月20日 北京马拉松
2015年9月13日 天津马拉松(北马意料之中的没有中签,很遗憾,首马不是在北京。)

训练周期
2015年7月20日~2015年9月20日,两个月

目标
时间:4:30
配速:6:00

配速训练
高速跑:4:30
中速跑:5:30
低速跑:6:30
配速跑:最终6:00

训练次数
每周六次,一三五力量,二四六跑步,周日休息。

训练量
一次高速跑3~5公里
一次中速的8~15公里
一次低速的LSD(15K、20K、25K、30K、35K、25K、20K,15K)最后两周减量
力量训练:平板撑、靠墙站、俯卧撑、仰卧起坐、拱桥、分腿半蹲

其他训练(2015年8月7日修改)
周二、周四 跳绳3000
周三、周五 囚徒三艺,俯卧撑(上肢)、深蹲(下肢)、拱桥(腰部)
周日 LSD 20 公里

速度
距离越短,速度越快
最后训练LSD的配速要达到 6:00