Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion
http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void printancestor(node *root, int key) {
if (!root) return;
stack<node*> S;
while () {
while (root && root->data != key) {
S.push(root);
root = root->left;
}
if (root && root->data == key) break;
if (S.top()->right == NULL) {
root = S.top();
S.pop();
while (!S.empty() && S.top()->right == root) {
root = S.top();
S.pop();
}
}
root = S.empty()? NULL : S.top()->right;
}
while (!S.empty()) {
cout << S.top()->data << " ";
S.pop();
}
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
root->left->left->left = new node();
root->left->right->right = new node();
root->right->right->left = new node();
for (int i = ; i < ; i++) {
printancestor(root, i);
cout << endl;
}
return ;
}
Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion的更多相关文章
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- [Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...
- [Data Structure] Tree - relative
Segment Tree First, try to build the segment tree. lintcode suggest code: Currently recursion recomm ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
随机推荐
- mysql :安装过程中的问题
安装mysql 选择路径的问题 出现如下问题:说明之前安装过mysql,写在的不干净
- C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)
需要下载ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; u ...
- unity4.6 Beta版 UI控件之Button
近期需求,须要用到4.6版本号uGui了,所以抽时间来学习学习,就UI控件在Unity工具里创建预设这块来说相比較于NGUI,我认为是没有什么太大的差别的. 比方:Canvas--Camera . T ...
- H5 打开App
IOS 1.只能在safari浏览器打开, 2.必须App安装的情况下. 3.IOS必须配置 URLSchemes 为 "yidiandai://" 才可以. <!DOCT ...
- Hibernate学习之单向一对多映射
© 版权声明:本文为博主原创文章,转载请注明出处 说明:该实例是通过映射文件和注解两种方式实现的.可根据自己的需要选择合适的方式 实例: 1.项目结构 2.pom.xml <project xm ...
- dedecms增加自定义表单管理员
打开\dede\inc\grouplist.txt 添加 >>自定义表单 >f_List>列出表单 >f_New>新建表单 >f_Edit>编辑表单 & ...
- 【ubantu】Ubuntu的一些常用快捷键
Ubuntu操作基本快捷键* 打开主菜单 = Alt + F1* 运行 = Alt + F2* 显示桌面 = Ctrl + Alt + d* 最小化当前窗口 = Alt + F9* 最大化当前窗口 = ...
- usermod命令
usermod 功能: 修改用户 常用参数:-c 账号说明-d 账号家目录-e 密码失效日期-g 主用户组GID-G 次用户组GID-l 账号名称-s she ...
- 在ListView的GroupItem头中显示每列的Summary
问题描述 WPF自带的ListView和DataGrid控,都提供了数据分组的支持,并可以对分组的Header进行自定义.但是,如果想在每个分组的Header中,显示出本分组的"小计&quo ...
- Office 365系列(二) -一些比较容易混淆的概念
上一篇比较简明地说了Office 365怎么注册使用,在继续探讨之前先讨论一些比较容易混淆的概念! 1. Office 365: 是微软云计划的一部分包括Exchange online, Lync ...