# 二叉樹的邊界遍歷

## 例

`2 3 5 6 4 7`

`5 7 9 1 4 3`

## C ++代碼打印二叉樹的邊界遍歷

```#include<bits/stdc++.h>
using namespace std;

struct node {
int data;
node *left, *right;
};

// print the leaves (nodes from the bottom)
void printLeaves(node* root)
{
if(root){
// recursively solve the problem for the left subtree
printLeaves(root->left);
// if current node is a leaf
if ((root->left) == NULL && (root->right) == NULL)
cout<<root->data<<" ";
// recursively solve the problem for right subtree
printLeaves(root->right);
}
}

// print all the nodes of left boundary
// this function will not print the leaves viewed from left side
void printLeft(node* root)
{
if(root){
if(root->left != NULL){
cout<<root->data<<" ";
// recursively move down the left subtree
printLeft(root->left);
}
else if(root->right){
cout<<root->data<<" ";
// recursively move down the right subtree
printLeft(root->right);
}
}
}

// print all the nodes of right boundary
// this function will not print the leaves viewed from the right side
void printRight(node* root)
{
// printing is done after moving down the tree
// thus printing is done in bottom up manner
if(root){
if (root->right) {
printRight(root->right);
cout<<root->data<<" ";
}
else if (root->left) {
printRight(root->left);
cout<<root->data<<" ";
}
}
}

void boundaryUtil(node* root)
{
// first print the root, then print the left boundary
// then the leaves which are seen from bottom
// at last print the right boundary
if(root){
cout<<root->data<<" ";
printLeft(root->left);
printLeaves(root->left);
printLeaves(root->right);
printRight(root->right);
}
}

node* create(int data){
node* tmp = new node();
tmp->data = data;
tmp->left = tmp->right = NULL;
return tmp;
}

int main()
{
node* root = create(5);
root->left = create(7);
root->right = create(3);
root->left->left = create(9);
root->left->right = create(6);
root->left->right->left = create(1);
root->left->right->right = create(4);

boundaryUtil(root);
}
```
`5 7 9 1 4 3`

## Java代碼打印二叉樹的邊界遍歷

```import java.util.*;

class node{
int data;
node left, right;
}

class Main{

// print the leaves (nodes from the bottom)
static void printLeaves(node root)
{
if(root != null){
// recursively solve the problem for the left subtree
printLeaves(root.left);
// if current node is a leaf
if ((root.left) == null && (root.right) == null)
System.out.print(root.data+" ");
// recursively solve the problem for right subtree
printLeaves(root.right);
}
}

// print all the nodes of left boundary
// this function will not print the leaves viewed from left side
static void printLeft(node root)
{
if(root != null){
if(root.left != null){
System.out.print(root.data+" ");
// recursively move down the left subtree
printLeft(root.left);
}
else if(root.right != null){
System.out.print(root.data+" ");
// recursively move down the right subtree
printLeft(root.right);
}
}
}

// print all the nodes of right boundary
// this function will not print the leaves viewed from the right side
static void printRight(node root)
{
// printing is done after moving down the tree
// thus printing is done in bottom up manner
if(root != null){
if(root.right != null){
printRight(root.right);
System.out.print(root.data+" ");
}
else if(root.left != null){
printRight(root.left);
System.out.print(root.data+" ");
}
}
}

static void boundaryUtil(node root)
{
// first print the root, then print the left boundary
// then the leaves which are seen from bottom
// at last print the right boundary
if(root != null){
System.out.print(root.data+" ");
printLeft(root.left);
printLeaves(root.left);
printLeaves(root.right);
printRight(root.right);
}
}

static node create(int data){
node tmp = new node();
tmp.data = data;
tmp.left = tmp.right = null;
return tmp;
}

public static void main(String[] args){
node root = create(5);
root.left = create(7);
root.right = create(3);
root.left.left = create(9);
root.left.right = create(6);
root.left.right.left = create(1);
root.left.right.right = create(4);

boundaryUtil(root);
}
}```
`5 7 9 1 4 3`