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 ...
随机推荐
- myeclipse svn安装
安装subclipse, SVN 插件 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigris.org, 2.从中解压出features与plugins文件夹,复制到 ...
- 下拉刷新Listview(8.30)
Android-PullToRefresh 1项目托管地址: https://github.com/bavariama1/Android-PullToRefresh 2 快速开始教程:https:// ...
- 手动建立storybook
1. Add @storybook/react npm i --save-dev @storybook/react 2. Add react, react-dom, and babel-core np ...
- thinkphp5.0极速搭建restful风格接口层实例
作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...
- java.long中的类和方法
java.lang.Character.isUpperCase(char ch) 确定指定的字符是否为大写字符. java.lang.Character.toUpperCase()方法用法转换从Uni ...
- 清华EMBA课程系列思考之三 -- 中国经济与金融
清华EMBA的第三次课,大家都已经渐渐了解了课程系列的基本节奏,也逐步适应了思考的基本思路,本次课程涉及到的全部内容都非常专业.闲话少述,直入主题了. 李稻葵教授部分: -- 清华大学经济管理学院弗里 ...
- Android环境变量的设置(详细图解版)
分类: Android初学学习笔记2011-07-10 09:47 99479人阅读 评论(0) 收藏 举报 androidtoolspathcmd 查阅了网上很多的资料但是对于环境变量设置介绍的不够 ...
- Apache + Tomcat集群 + 负载均衡
Part I: 取经处: http://www.ramkitech.com/2012/10/tomcat-clustering-series-simple-load.html http://blog ...
- PHP插入法排序
/** 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法. 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描, 找到相应位置并插入.插入排序在实现上 ...
- iOS js oc相互调用(JavaScriptCore)---js调用iOS --js里面通过对象调用方法
下来我们看第二种情况 就是js 中是通过一个对象来调用方法的. 此处稍微复杂一点我们需要使用到 JSExport 凡事添加了JSExport协议的协议,所规定的方法,变量等 就会对js开放,我们可以通 ...