49. 把字符串转换为整数

很多细节需要注意。(空格,符号,溢出等)

Go: 8. String to Integer (atoi)

50. 树种两个结点的最低公共祖先

A. 若是二叉搜索树,直接与根结点对比。 若都大于根节点,则在友子树;若都小于根节点,则在左子树;若根节点介于两数之间,则根节点即为答案。

B. 普通树,若是孩子节点有指向父节点的指针,则问题变为求两个链表的第一个公共结点。 如:37题。

C. 普通树:思路1,若一个结点的子树同时包含两个结点,而它的任一孩子结点的子树却不能同时包含,则该节点即为答案。需要重复遍历,时间复杂度较高。

思路2:先序优先遍历,分别记录从根节点到两个结点的路径。然后转换为求第一个公共结点问题。

#include <iostream>
#include <string>
#include <list>
using namespace std; typedef struct Node
{
char v; // In this code, default positive Integer.
Node *child[3];
Node(char x) : v(x){ child[0] = NULL; child[1] = NULL;child[2] = NULL; }
} Tree;
typedef list<Node*> PATH;
/********************************************************/
/***** Basic functions for tree ***********/
Tree* createTree() // input a preOrder sequence, 0 denote empty node.
{
Node *pRoot = NULL;
char r;
cin >> r;
if(r != '0') // equal to if(!r) return;
{
pRoot = new Node(r);
for(int i = 0; i < 3; ++i)
pRoot->child[i] = createTree();
}
return pRoot;
}
void printTree(Tree *root, int level = 1){
if(root == NULL) { cout << "NULL"; return; };
string s;
for(int i = 0; i < level; ++i) s += "\t";
printf("%c", root->v);
for(int i = 0; i < 3; ++i)
{
cout << endl << s;
printTree(root->child[i], level+1);
}
}
void releaseTree(Tree *root){
if(root == NULL) return;
for(int i = 0; i < 3; ++i)
releaseTree(root->child[i]);
delete[] root;
root = NULL;
}
/******************************************************************/
/**** 获取第一个公共父节点 ******/
bool getPath(Tree *root, Node *node, PATH& path)
{
if(root == NULL) return false;
path.push_back(root);
if(root == node)
return true;
bool found = false;
for(int i = 0; i < 3; ++i)
{
found = getPath(root->child[i], node, path);
if(found) break;
}
if(!found) path.pop_back();
return found;
} Node* getLastCommonNode(const PATH &path1, const PATH &path2) // get the last common node of two lists
{
Node *father = NULL;
for(auto it1 = path1.begin(), it2 = path2.begin(); it1 != path1.end() && it2 != path2.end(); ++it1, ++it2)
{
if(*it1 == *it2) father = *it1;
else break;
}
return father;
} Node* getFirstCommonFather(Tree *root, Node *node1, Node *node2)
{
if(root == NULL || node1 == NULL || node2 == NULL) return NULL;
PATH path1, path2;
if(getPath(root, node1, path1) && getPath(root, node2, path2))
return getLastCommonNode(path1, path2);
return NULL;
}
/************************************************************************/
int main(){
int TestTime = 3, k = 1;
while(k <= TestTime)
{
cout << "Test " << k++ << ":" << endl; cout << "Create a tree: " << endl;
Node *pRoot = createTree();
printTree(pRoot);
cout << endl; Node *node1 = pRoot->child[0]->child[0]->child[1];
Node *node2 = pRoot->child[0]->child[2]->child[0];
Node *father; father = getFirstCommonFather(pRoot, node1, node2);
cout << "the first common father node: " << father->v << endl;
releaseTree(pRoot);
}
return 0;
}

Chapter7: question 49 - 50的更多相关文章

  1. 49. 3种方法实现复杂链表的复制[clone of complex linked list]

    [本文链接] http://www.cnblogs.com/hellogiser/p/clone-of-complex-linked-list.html [题目] 有一个复杂链表,其结点除了有一个ne ...

  2. ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)

    [热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...

  3. 高质量PHP代码的50个实用技巧必备(下)

    26. 避免直接写SQL, 抽象之 不厌其烦的写了太多如下的语句: ? 1 2 <span style="color:#333333;font-family:''Helvetica, ...

  4. ORA-03137: TTC 协议内部错误: [12333] [4] [49] [51] [] [] [] []

    [1]问题背景:Oracle数据库版本为11.2.0.1,操作系统CentOS release 5.9,详细的报错信息如下: Dump file /data/oracle/diag/rdbms/db0 ...

  5. 最有价值的50道java面试题 适用于准入职Java程序员

    下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...

  6. 50道基础的java面试题

    Java程序员面试题集(1-50) 一.Java基础部分 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象 ...

  7. 50.AngularJs directive详解及示例代码

    转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...

  8. Java经典逻辑编程50题

    Java经典逻辑编程50题 2016-11-03 09:29:28      0个评论    来源:Alias_fa的博客    收藏   我要投稿 [程序1] 題目:古典问题:有一对兔子,从出生后第 ...

  9. 使用Unity的50个建议

    关于这些建议 这些建议并不适用于所有的项目 这些建议是基于我与3-20人的小团队项目经验总结出来的 结构.可重复使用性.明晰度都是有价的——团队规模和项目规模决定了是否值得付这个价. 一些建议也许公然 ...

随机推荐

  1. Spring配置中的classpath和classpath*的区别

    初学SSH框架,昨天在配置Spring的时候,提示我找不到Spring.xml文件,后面百度之后把classpath改成classpath* 就好了,下面是了解到的简单区别. classpath:只会 ...

  2. iMacros 教程

    imacros能记录你在网页中的动作,然后模拟你的动作自动重复执行.进阶应用主要在于两个方面: 1.用JS动态调用,重复执行之. 2.调用CSV文件,这个不错哦. 还可以调用数据库,这个没用过. 安装 ...

  3. SPFA算法学习笔记

    一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...

  4. mysql 日期类型比较

    MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型        存储空间       日期格式                 日期范围 ------------ ------ ...

  5. hdu 2073

    ps:感觉纯数学题.....我的思路就是找规律,发现dp(x+y) 就是 (0,x+y)的值,然后再加上x*根号2.就是他的距离.然后两个距离相减的绝对值就是两个点之间的距离了. 代码: #inclu ...

  6. zip函数

    zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个包含元组的列表. x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) ...

  7. Java语言的多态性

    用简单的话来描述Java:编译类型与运行类型不一致的时候就会出现多态! 下面一段代码可以用来描述Java多态 class BaseClass{ public String flag="父类的 ...

  8. android 检测网络是否连接,或者GPS是否可用

    很多android程序在打开时,检测网络是否连接,或者GPS是否可用: 1.网络是否连接(包括Wifi和移动网络) // 是否有可用网络 private boolean isNetworkConnec ...

  9. 第二个Sprint冲刺项目github

    https://github.com/22shaojiawen/the-second-sprint-project

  10. java中时间类型的问题

    时间类型:System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为longimport java.sql.Date d ...