cout<<root->data<<' ';
57 }
58 }
59
60 int NodeNumber(Node root)
61 {
62 if(root == NULL)
63 return 0;
64 else
65 return 1+NodeNumber(root->lchild) + NodeNumber(root->rchild);
66 }
67
68 int DepthTree(Node root)
69 {
70 if(root)
71 return DepthTree(root->lchild) > DepthTree(root->rchild) ? DepthTree(root->lchild) + 1 : DepthTree(root->rchild) + 1;
72 else
73 return 0;
74 }
75
76 int LeafNumber(Node root)
77 {
78 if(!root)
79 return 0;
80 else if( (root->lchild == NULL) && (root->rchild == NULL) )
81 return 1;
82 else
83 return ( LeafNumber(root->lchild) + LeafNumber(root->rchild) );
84 }
85
86
87
88 int main()
89 {
90 Node root=NULL;
91 cout<<"请输入数据:"<<endl;
92 root=CreatTree();
93 cout<<"二叉树建立成功!"<<endl;
94
95 cout<<"二叉树节点数为:"<<NodeNumber(root)<<endl;
96
97 cout<<"二叉树深度为:"<<DepthTree(root)<<endl;
98
99 cout<<"二叉树叶子结点数为:"<<LeafNumber(root)<<endl;
100
101 cout<<"前序遍历:"<<endl;
102 PreorderTraverse(root);
103 cout<<endl;
104
105 cout<<"中序遍历:"<<endl;
106 InorderTraverse(root);
107 cout<<endl;
108
109 cout<<"后序遍历:"<<endl;
110 LastorderTraverse(root);
111 cout<<endl;
112
113 return 0;
114 }
?
首发在CSDN,有兴趣可以访问https://blog.csdn.net/qq_41785863
|