先序遍历二叉树,查找第k个节点

先序遍历二叉树,查找第k个节点

先序遍历二叉树第k个节点

#define ERROR 0
#define OK 1
int con = 0;//全局变量

Status FindNode(BiTree T, int k)
{
	if(T == NULL) return ERROR;
	else
	{
		con++;
		if(con == k)
		{
			cout << T->data;
			return OK;
		}
		FindNode(T->lChild, k);
		FindNode(T->rChild, k);
	}
}
好的,我知道了。以下是用C++实现二叉树的基本操作的代码: ```c++ #include <iostream> using namespace std; struct TreeNode { char val; TreeNode* left; TreeNode* right; TreeNode(char x) : val(x), left(NULL), right(NULL) {} }; // 建立二叉树 void createTree(TreeNode* &root) { char val; cin >> val; if (val == '#') { root = NULL; } else { root = new TreeNode(val); createTree(root->left); createTree(root->right); } } // 先序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } cout << root->val << ' '; preorderTraversal(root->left); preorderTraversal(root->right); } // 中序遍历 void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); cout << root->val << ' '; inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << ' '; } // 计算深度 int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return max(leftDepth, rightDepth) + 1; } // 计算节点数 int countNodes(TreeNode* root) { if (root == NULL) { return 0; } int leftNodes = countNodes(root->left); int rightNodes = countNodes(root->right); return leftNodes + rightNodes + 1; } // 计算叶子节点数 int countLeaves(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } int leftLeaves = countLeaves(root->left); int rightLeaves = countLeaves(root->right); return leftLeaves + rightLeaves; } // 查找值=x的节点的个数 int countValue(TreeNode* root, char x) { if (root == NULL) { return 0; } int count = 0; if (root->val == x) { count++; } count += countValue(root->left, x); count += countValue(root->right, x); return count; } // 以缩格文本形式输出所有节点 void printTree(TreeNode* root, int depth) { if (root == NULL) { return; } printTree(root->right, depth + 1); for (int i = 0; i < depth; i++) { cout << " "; } cout << root->val << endl; printTree(root->left, depth + 1); } int main() { TreeNode* root = NULL; char command; while (cin >> command) { if (command == 'C') { createTree(root); } else if (command == '1') { preorderTraversal(root); cout << endl; } else if (command == '2') { inorderTraversal(root); cout << endl; } else if (command == '3') { postorderTraversal(root); cout << endl; } else if (command == 'H') { cout << maxDepth(root) << endl; } else if (command == 'L') { cout << countLeaves(root) << endl; } else if (command == 'N') { cout << countNodes(root) << endl; } else if (command == 'F') { char x; cin >> x; cout << countValue(root, x) << endl; } else if (command == 'P') { printTree(root, 0); } } return 0; } ``` 你可以先输入C,然后按照先序遍历的顺序输入二叉树节点,其中#表示空节点。然后,你可以输入相应的命令来进行二叉树的操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值