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. ABAP遇到的问题——1

    在创建ABAP对象的时候抛出“测试对象不能被创建在外来命名空间”的错误 原因:程序的名字不是以Z或者Y开头的.

  2. 为speedphp最新版添加 仿Yii 的简易版 数据验证 支持不同场景,自定义回调

    给个意见或建议吧 扩展一个Model基类 <?php class BaseModel extends Model{ use ValidationRules; public function ru ...

  3. [TD Cup 2014] TDL的YC牌 & TDL的幼儿园

    TDL的YC牌 传说中的置换群?反正不懂.我的思路竟然是对的,可是为何只有20分? (1)尼玛每行数据输出后回车不打! (2)写gcd函数脑残把a mod b写成a-b,大大减慢速度… (3)看标程才 ...

  4. 后台返回JSON关于日期的格式化

    JSONObject 可以将java对象转换成json格式,用于处理ajax请求或者做app是与前台的交互. 但是Date类型的也会做转换,很多时候我们是不想将日期的年月日分别转换成json的.可以通 ...

  5. HDU 1538

    http://acm.hdu.edu.cn/showproblem.php?pid=1538 经典经济学问题,海盗分金 分析http://www.guokr.com/article/41423/ #i ...

  6. Win10/UWP新特性系列—电池报告

    UWP中,新增了当节电模式开启时,App能获取到通知的API,通过响应电源条件的更改,比如咨询用户是否使用黑色背景等来帮助延长电池使用时间. 通过Windows.Devices.Power命名空间中的 ...

  7. sqlserver08评估期已过的解决方法

    打开sqlserver出现提示:评估期已过.有关如何升级的测试版软件的信息,请访问http://www.microsoft.com/sql/howtobuy 解决方法如下: 第一步:进入开始菜单--- ...

  8. MATLAB与C/C++混合编程的一些总结

    [转载请注明出处]http://www.cnblogs.com/mashiqi 先上总结: 由于C/C++语言的函数输入输出参数的特点,可以将多个参数方便地传入一个函数中,但却不能方便地返回多个参数. ...

  9. CCLabel在最大宽度已知的情况下如何获取实际宽高

    当前环境在cocos2.2.6, 在UI摆图中,会遇到一种情况就是 设定了label的最大宽度MAX_WIDTH,但label的内容是动态的,如何在label输入了文字之后获取label的真实宽高? ...

  10. C++中的迭代器

    C++STL中的迭代器 "指针"对所有C/C++的程序员来说,一点都不陌生.在接触到C语言中的malloc函数和C++中的new函数后,我们也知道这两个函数返回的都是一个指针,该指 ...