Cracking The Coding Interview4.5
- //原文:
- //
- // Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.
- //
- //译文:
- //
- // 给定二叉查找树的一个结点, 写一个算法查找它的“下一个”结点(即中序遍历后它的后继结点), 其中每个结点都有指向其父亲的链接。
- // 下个节点为父节点或者右子树的最左叶子
- #include <iostream>
- #include <string>
- #include <queue>
- #include <stack>
- #include <list>
- using namespace std;
- struct treenode
- {
- char data;
- treenode * left;
- treenode * right;
- treenode * parent;
- };
- class tree
- {
- public:
- tree()
- {
- //root = create();
- root = NULL;
- index = 0;
- }
- ~tree()
- {
- /***清空二叉树***/
- }
- /***二叉排序树:插入。***/
- void insert(char *s)
- {
- if (s == NULL)
- {
- return;
- }
- int size = strlen(s);
- for (int i = 0; i<size; i++)
- {
- insert(&root, s[i], NULL);
- }
- }
- void levelOrder()
- {
- queue<treenode *> q;
- if (root != NULL)
- {
- q.push(root);
- }
- while(!q.empty())
- {
- treenode *t = q.front();
- cout<<t->data<<endl;
- q.pop();
- if (t->left != NULL)
- {
- q.push(t->left);
- }
- if (t->right != NULL)
- {
- q.push(t->right);
- }
- }
- }
- treenode * findNext(treenode *p)
- {
- if (p->right == NULL)
- {
- return p->parent;
- }
- else
- {
- treenode *s = p->right;
- while(s->left != NULL)
- {
- s = s->left;
- }
- return s;
- }
- }
- void preOrder(){ pOrder(root);}
- void inOreder(){ zOrder(root);}
- void postOreder(){ hOrder(root);}
- treenode *root;
- private:
- int index;
- void insert(treenode **p, char s, treenode *parent)
- {
- if (((*p) == NULL) && s != '\0')
- {
- *p = new treenode;
- (*p)->data = s;
- (*p)->left = NULL;
- (*p)->right = NULL;
- (*p)->parent = parent;
- }
- else
- {
- if ((*p)->data > s)
- {
- insert(&((*p)->left) , s, *p);
- }
- else
- {
- insert(&((*p)->right) , s, *p);
- }
- }
- }
- void pOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- cout<<p->data<<" "<<endl;
- pOrder(p->left);
- pOrder(p->right);
- }
- void zOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- zOrder(p->left);
- cout<<p->data<<" "<<endl;
- zOrder(p->right);
- }
- void hOrder(treenode *p)
- {
- if (p==NULL)
- {
- return;
- }
- hOrder(p->left);
- cout<<p->data<<" "<<endl;
- hOrder(p->right);
- }
- };
- int main()
- {
- /**非递归层次遍历*************************/
- tree s;
- char t[8] = "3289654";
- s.insert(t);
- //s.levelOrder();
- treenode *p =s.findNext((s.root)->right);
- cout<<p->data<<endl;
- return 0;
- }
Cracking The Coding Interview4.5的更多相关文章
- Cracking The Coding Interview4.8
//You are given a binary tree in which each node contains a value. Design an algorithm to print all ...
- Cracking The Coding Interview4.3
//Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
随机推荐
- (转)基于C#的socket编程的TCP异步实现
一.摘要 本篇博文阐述基于TCP通信协议的异步实现. 二.实验平台 Visual Studio 2010 三.异步通信实现原理及常用方法 3.1 建立连接 在同步模式中,在服务器上使用Accept方法 ...
- Polygenic score
We estimate the maximum prediction accuracy for the risk of Alzheimer's disease based on disease pre ...
- Bulk RNA-Seq转录组学习
与之对应的是single cell RNA-Seq,后面也会有类似文章. 参考:https://github.com/xuzhougeng/Learn-Bioinformatics/ 作业:RNA-s ...
- .net WinForm 的数据绑定
.net WinForm 的数据绑定相当灵活 http://www.cnblogs.com/ydong/archive/2006/04/22/381847.html 原来只知道 Control 类上的 ...
- LeetCode--155--最小栈(java版)
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...
- 20190102xlVBA_多表按姓名同时拆分
Sub 多表按姓名同时拆分20190102() AppSettings Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA ...
- 1. windows 下redis数据库的安装
安装 window系统的redis是微软团队根据官方的linux版本高仿的 官方原版: https://redis.io/ 中文官网:http://www.redis.cn 下载地址: https:/ ...
- Alyona and a tree CodeForces - 739B (线段树合并)
大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...
- 『TensotFlow』RNN中文文本_下_暨研究生开学感想
承前 接上节代码『TensotFlow』RNN中文文本_上, import numpy as np import tensorflow as tf from collections import Co ...
- [java]转:String Date Calendar之间的转换
String Date Calendar之间的转换 String Date Calendar 1.Calendar 转化 String Calendar calendat = Calendar.ge ...