Data Structure Binary Tree: Boundary Traversal of binary tree
2024-10-19 12:02:33
http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
#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 printleft(node *root) {
if (!root) return;
if (root->left) {
cout << root->data << " ";
printleft(root->left);
}
else if (root->right) {
cout << root->data << " ";
printleft(root->right);
}
} void printleaf(node *root) {
if (!root) return;
printleaf(root->left);
if (!root->left && !root->right) cout << root->data << " ";
printleaf(root->right);
} void printright(node *root) {
if (!root) return;
if (root->right) {
printright(root->right);
cout << root->data << " ";
}
else if (root->left) {
printright(root->left);
cout << root->data << " ";
}
} void printboundary(node *root) {
if (!root) return;
cout << root->data << " ";
printleft(root->left);
printleaf(root);
printright(root->right);
} 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->right = new node();
root->left->right->left = new node();
root->left->right->right = new node();
printboundary(root);
return ;
}
最新文章
- mac上的git环境配置
- [Ng]Angular应用点概览
- 使用WinDbg调试SQL Server——入门
- [Hibernate] - Interceptors and events
- [kuangbin带你飞]专题九 连通图
- win7 C# winForm编程 savefiledialog 不能弹出保存窗体
- JS里面匿名函数的调用 &; 变量作用域的实验
- codeforces 680B B. Bear and Finding Criminals(水题)
- VBA.NET 系统可行性分析模板
- 【转】priority_queue优先队列
- Fiddler抓包【7】_次要功能和第三方插件
- Linux将rm命令设置为回收站【转】
- html5 javascript 表单练习案例
- 洛谷P2053 修车
- 【colaboratory】ModuleNotFoundError: No module named &#39;forward&#39;
- PHP安全性考虑
- java线程池(一)
- 【uoj#164】[清华集训2015]V 线段树维护历史最值
- C++ 函数的扩展②
- colspan和rowspan合并单元格
热门文章
- 奥巴马(Obama)获胜演讲全文[中英对照]+高清视频下载
- unity4.6 Beta版 UI控件之Button
- mysql负载飙高原因分析
- 支付宝开放平台 配置RSA(SHA1)密钥 OpenSSL配置公钥私钥对
- [译]GLUT教程 - 创建和关闭子窗体
- Android使用SQLite数据库
- simple_pool对象池——优化&;lt;二&;gt;
- wps文档怎样去除广告
- iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客
- Linux进程间通信(一) - 管道