打印二叉树的深度



这道题本质上还是考查二元树的遍历

如果一棵树只有一个结点,它的深度为1。如果根结点只有左子树而没有右子树,那么树的深度应该是其左子树的深度加1;同样如果根结点只有右子树而没有左子树,那么树的深度应该是其右子树的深度加1。如果既有右子树又有左子树呢?那该树的深度就是其左、右子树深度的较大值再加1

上面的这个思路用递归的方法很容易实现,只需要对遍历的代码稍作修改即可

参考资料:剑指offer

代码:

#include <iostream>
using namespace std;

typedef struct node{
	int data;
	struct node *lchild;
	struct node *rchild;
}Node ,*pNode;

void createTree(pNode & root){
	int temp;
	scanf("%d",&temp);
	if(0 != temp){
		root=(pNode) malloc (sizeof(Node));
		root->data = temp;
		createTree(root->lchild);
		createTree(root->rchild);
	}else{
		root=NULL;
	}
	
}

void print(const pNode root){
    pNode p = root;
	if(p){
		cout<< p->data<< " ";
		print(p->lchild);
		print(p->rchild);
	}
	
}
//类似先序遍历,节点会重复访问,效率较低
int btDepth(const pNode  root){
	if(NULL == root)
		return 0;
	int left = btDepth(root->lchild);
	int right = btDepth(root->rchild);
	return left > right ? left + 1 : right + 1;
}

//类似后序遍历,效率比较高,不会重复访问节点
void  btDepth(const pNode  root,int &depth){
	if(NULL == root){
		depth=0;
		return ;
	}
	int left ;
	btDepth(root->lchild,left);
	
    int right ;
	btDepth(root->rchild,right);
	depth = left > right ? left +1 : right +1;
}
int main(){
	pNode root=NULL;
	createTree(root);
	print(root);
	cout<<endl;
	cout<<btDepth(root);
	int depth=0;
	btDepth(root,depth);
	cout<<endl<<depth;
	return 0;

}

二叉树测试:


运行结果:


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的打印叉树结构的C语言代码,可以将二叉树以树形结构打印出来: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; void printTree(TreeNode* root, int depth) { if (root == NULL) { return; } // 先递归打印右子树 printTree(root->right, depth + 1); // 打印当前节点 for (int i = 0; i < depth; i++) { printf(" "); } printf("%d\n", root->val); // 再递归打印左子树 printTree(root->left, depth + 1); } int main() { // 构造一个二叉树 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->left->val = 2; root->right->val = 3; root->left->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->right = (TreeNode*)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right->val = 5; root->left->left->left = NULL; root->left->left->right = NULL; root->left->right->left = NULL; root->left->right->right = NULL; root->right->left = NULL; root->right->right = NULL; // 打印叉树 printTree(root, 0); // 释放内存 free(root->left->left); free(root->left->right); free(root->right); free(root->left); free(root); return 0; } ``` 上述代码中,我们定义了一个 `TreeNode` 结构体表示二叉树节点,其中包含了节点值、左右子树指针。`printTree` 函数用于递归打印叉树,其中需要传入当前节点和当前节点所在的深度。在递归过程中,我们先递归打印右子树,再打印当前节点,最后递归打印左子树。打印当前节点时,我们需要根据节点深度打印一定数量的空格,以便呈现树形结构。最后,我们在 `main` 函数中构造了一个简单的二叉树,并调用 `printTree` 函数将其打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值