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. ES6 基础版迭代器

    ES6中引入了generator function* get() { var result1 = yield c; var result2 = yield b; var result3 = yield ...

  2. CSipSimple结构浅析

    最近做一个VOIP的项目,调研了CSipSimple.都说CSipSimple结构清晰,但是代码下下来看了一下,还是一头雾水,不知从何看起.于是想到从最简单的打电话开始,借助网上一篇博文"C ...

  3. SQLite简单使用说明

    System.Data.SQLite.dll下载地址 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 选择. ...

  4. Mytophome Deal

    using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...

  5. ios开发之数据存储

    iOS应用数据存储的常用方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data 应用沙盒 ...

  6. goldengate 12c 12.2 新特性(updated)

    GoldenGate 12.2已经提供下载,增加了不少新特性 1. 异构配置加强不在需要sourceDefs和AssumeTargetDefs文件,在队列文件中已经包含metadata信息,比如tab ...

  7. 2014年4月份第3周51Aspx源码发布详情

    WPY净水机网站源码  2014-4-14 [VS2008]源码描述: 实现产品展示,在线留言,信息发布,在线咨询,营销网络地图. 网站基本管理:网站banner管理 管理首页滚动图片信息 网站右下部 ...

  8. github的注册过程

    带着疑问打开了github.这是一个神奇的网站,因为它到处都是英语,对于我这种英语盲这简直太痛苦了.借助了百度翻译,我还是马马虎虎的完成了github的制作. 首先在它的登录界面下面有一个sign u ...

  9. 【three.js详解之二】渲染器篇

    [three.js详解之二]渲染器篇   本篇文章将详细讲解three.js中渲染器(renderer)的设置方法. three.js文档中渲染器的分支如下: Renderers CanvasRend ...

  10. Android 学习第3课,小例子

    package temperature.convert; import java.util.Scanner; public class Converter { public static void m ...