Chapter7: question 49 - 50
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的更多相关文章
- 49. 3种方法实现复杂链表的复制[clone of complex linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/clone-of-complex-linked-list.html [题目] 有一个复杂链表,其结点除了有一个ne ...
- ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)
[热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...
- 高质量PHP代码的50个实用技巧必备(下)
26. 避免直接写SQL, 抽象之 不厌其烦的写了太多如下的语句: ? 1 2 <span style="color:#333333;font-family:''Helvetica, ...
- ORA-03137: TTC 协议内部错误: [12333] [4] [49] [51] [] [] [] []
[1]问题背景:Oracle数据库版本为11.2.0.1,操作系统CentOS release 5.9,详细的报错信息如下: Dump file /data/oracle/diag/rdbms/db0 ...
- 最有价值的50道java面试题 适用于准入职Java程序员
下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...
- 50道基础的java面试题
Java程序员面试题集(1-50) 一.Java基础部分 1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: 1)抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象 ...
- 50.AngularJs directive详解及示例代码
转自:https://www.cnblogs.com/best/tag/Angular/ 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github ...
- Java经典逻辑编程50题
Java经典逻辑编程50题 2016-11-03 09:29:28 0个评论 来源:Alias_fa的博客 收藏 我要投稿 [程序1] 題目:古典问题:有一对兔子,从出生后第 ...
- 使用Unity的50个建议
关于这些建议 这些建议并不适用于所有的项目 这些建议是基于我与3-20人的小团队项目经验总结出来的 结构.可重复使用性.明晰度都是有价的——团队规模和项目规模决定了是否值得付这个价. 一些建议也许公然 ...
随机推荐
- CentOS修改默认编码为UTF-8,使java程序字符集默认为UTF-8
java程序在本地接受php的utf8字符串好好的,到了服务器就行了. 解决,修改vi /etc/sysconfig/i18n,修改之后ssh断开,重连后KILL你的java. LANG=" ...
- Windows下gvim配置
Windows下gvim配置原作地:http://hi.baidu.com/leemoncc/blog/item/a6be15cf40d7ab31b600c806.html 0.准备软件及插件. (a ...
- sqoop的export导入到oracle中
- jQuery--index() window.onhashchange
index(): 1. 如果没有参数传给该函数,那么就返回一个整数,为其相对于其兄弟节点的位置. 2. 如果在一个元素集合上调用该函数,并且传入的参数为一个DOM元素或jQuery对象,那么返回一个整 ...
- 福州月赛2057 DFS
题意:告诉你族谱,然后Q条查询s和t的关系,妈妈输出M,爸爸输出F: 题目地址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=78233# ...
- 苹果IPhone手机由于更新了IOS7 Beta测试版导致“激活出错”后,如何还原电话本和照片方法
苹果这狗日的,手段果然狠,因为用户提前升级了测试版又没有更新正式版,就突然把手机变砖头,既不让升级正式版,也不让备份手机中的信息,确实有必要这样吗? 我的手机是IPone4s,在看了6月Apple W ...
- windows server 2008 r2 切换windows 7主题方法
1. 打开Powershell 里 Cmdlets 管理角色和功能Import-Module servermanager 2. 安装桌面体验Add-WindowsFeature Desktop-Exp ...
- High Precision Timers in iOS / OS X
High Precision Timers in iOS / OS X The note will cover the do's and dont's of using high precision ...
- 基于GRPC+consul通信的服务化框架(转)
原文:http://blog.csdn.net/yeyincai/article/details/51470475 -.背景 谈论服务化框架的时候,我们首先先了解这些概念:SOA.ESB.OSGi.s ...
- iar 数据类型 int folat
8位或者16位的MCU,int占2字节32位的ARM是4字节 则430 int为2字节,则最大值32767,最小值-32768.