#include <iostream>
using namespace std;
// binary tree node
struct Node
{
char data;
Node *left,*right;
Node(char x)
{
data = x;
left = right = NULL;
}
};
void RightNode(Node *root, bool f)
{
// empty node
if (!root)
return;
if (f) {
cout<<(root->data)<< "\t";
}
RightNode(root->left, false);
RightNode(root->right, true);
}
void LeftNode(Node *root, bool f)
{
// empty node
if (!root)
return;
if (f) {
cout<<(root->data)<< "\t";
}
LeftNode(root->left, true);
LeftNode(root->right, false);
}
// Driver code
int main()
{
Node *root = new Node('A');
root->left = new Node('B');
root->left->left = new Node('C');
root->left->right = new Node('D');
root->left->right->left = new Node('E');
root->right = new Node('F');
root->right->left = new Node('G');
root->right->left->left = new Node('H');
root->right->left->right = new Node('I');
root->right->right = new Node('J');
root->right->right->right = new Node('K');
cout << "Left Node :\t";
LeftNode(root, true);
cout << endl;
cout << "Right Node:\t";
RightNode(root, true);
cout << endl;
return 0;
}
输出结果:
Left Node : A B C E G H
Right Node: A D F I J K
//此处 根节点 即使 左节点, 也是右结点。