您的位置:首页 > Web前端 > Node.js

Print Boundry Nodes of a binary tree.

2016-06-16 05:11 507 查看
void printLeaves(TreeNode* root) {
if(root) {
printLeaves(root->left);
if(!(root->left) && !(root->right)) cout << root->val << endl;
printLeaves(root->right);
}
}

void PrintBoundryRight(TreeNode* root) {
if(root) {
if(root->right) {
PrintBoundryRight(root->right);
cout << root->val << endl;
} else if(root->left) {
PrintBoundryRight(root->left);
cout << root->val << endl;
}
}
}

void PrintBoundryLeft(TreeNode* root) {
if(root) {
if(root->left) {
cout << root->val << endl;
PrintBoundryLeft(root->left);
} else if(root->right) {
cout << root->val << endl;
PrintBoundryLeft(root->right);
}
}
}

void printBoundry(TreeNode* root) {
if(!root) return;
cout << root->val << endl;
PrintBoundryLeft(root->left);

printLeaves(root->left);
printLeaves(root->right);

PrintBoundryRight(root->right);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: