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);
}
Print Boundry Nodes of a binary tree.
最新推荐文章于 2023-12-30 05:01:57 发布
本文介绍了一种遍历二叉树的方法,通过三个辅助函数实现:打印叶节点、从左边界打印非叶节点及从右边界逆序打印非叶节点。这些函数共同完成对整棵树边界的遍历。
163

被折叠的 条评论
为什么被折叠?



