朱建国 发布的文章

首先,欢迎大家来“芹声勤语”做客。 芹声勤语专栏已设置许久,本想在开栏当天发表文章,但是因各种原因,到今天才有时间来写开篇辞。在此非常感谢博主给专栏命此名,实乃一莫大的惊喜,因本人名“勤”,昵称“芹”,博主的用心非常感动。

开此专栏的灵感来源于和博主的一次谈话。可能是人在经历很多事情之后,慢慢的学会了淡忘,当你习惯了这种淡忘以后,好多事情在脑海里只能找到模糊的印记,可却不能清晰回忆,特别是正在经历的“孕傻”时期。每天上班,工作,吸奶,下班,带娃,睡觉,这样快节奏的规律性的生活往往让一天的时间不知不觉的溜走,却回忆不起来过去的一天经历的有意义的事情,因为记忆已经习惯性的选择忘记,可是每天真的会有很多欣喜。人生在回忆过去的时候不应该是空洞无内容的,应该是充满感动或无奈、欣喜或悲、幸福或者不幸福的,那种经历过的存在感,在你回忆的时候应该是依旧能让你感动的。因此跟博主提出想要有一片地方,专门记录这些点点滴滴,当前行休息的时候,还可以看过去走过的路。

网络世界的你我他,生活中可能并无交集,但是却又紧密的联系在一起,欢迎大家来芹声勤语的小家,分享自己的点点滴滴。

题目:
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、第一次提交一次通过!好开心。