[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
Struct tnode
{
Int data;
Struct tnode * lchild;
Struct tnode * rchild;
} Tnode;
Typedef struct tnode * TNode;
// This program is useless.
TNode newNode (int data)
{
TNode node = (TNode) malloc (sizeof (struct tnode ));
Node-> data = data;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}
TNode createTree (TNode root)
{
Int data = 0;
Scanf ("% d", & data );
If (data =-1) return NULL;
Root = (TNode) malloc (sizeof (struct tnode ));
Root-> data = data;
Root-> lchild = NULL;
Root-> rchild = NULL;
Root-> lchild = createTree (root-> lchild); // if no value is assigned to root-> lchild, the left subtree is not created, and root-> lchild is still NULL.
Root-> rchild = createTree (root-> rchild );
Return root;
}
Void preorderTraverse (TNode root)
{
If (root! = NULL)
{
Printf ("% d", root-> data );
PreorderTraverse (root-> lchild );
PreorderTraverse (root-> rchild );
}
}
Int treeDepth (TNode root)
{
If (root = NULL)
Return 0;
Int nleft = treeDepth (root-> lchild );
Int nright = treeDepth (root-> rchild );
Return (nleft> nright )? (Nleft + 1): (nright + 1 );
}
Void treeFree (TNode root)
{
If (root! = NULL)
{
TreeFree (root-> lchild );
TreeFree (root-> rchild );
Printf ("% d", root-> data );
Free (root );
}
}
Void test ()
{
TNode root = NULL;
Root = createTree (root );
PreorderTraverse (root );
Printf ("\ n ");
Int depth = treeDepth (root );
Printf ("depth = % d \ n", depth );
TreeFree (root );
}
Int main ()
{
Test ();
Return 0;
}
/*****************************\
1
2 6
-1 5 4-1
-1-1-1-1
\*****************************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
Struct tnode
{
Int data;
Struct tnode * lchild;
Struct tnode * rchild;
} Tnode;
Typedef struct tnode * TNode;
// This program is useless.
TNode newNode (int data)
{
TNode node = (TNode) malloc (sizeof (struct tnode ));
Node-> data = data;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}
TNode createTree (TNode root)
{
Int data = 0;
Scanf ("% d", & data );
If (data =-1) return NULL;
Root = (TNode) malloc (sizeof (struct tnode ));
Root-> data = data;
Root-> lchild = NULL;
Root-> rchild = NULL;
Root-> lchild = createTree (root-> lchild); // if no value is assigned to root-> lchild, the left subtree is not created, and root-> lchild is still NULL.
Root-> rchild = createTree (root-> rchild );
Return root;
}
Void preorderTraverse (TNode root)
{
If (root! = NULL)
{
Printf ("% d", root-> data );
PreorderTraverse (root-> lchild );
PreorderTraverse (root-> rchild );
}
}
Int treeDepth (TNode root)
{
If (root = NULL)
Return 0;
Int nleft = treeDepth (root-> lchild );
Int nright = treeDepth (root-> rchild );
Return (nleft> nright )? (Nleft + 1): (nright + 1 );
}
Void treeFree (TNode root)
{
If (root! = NULL)
{
TreeFree (root-> lchild );
TreeFree (root-> rchild );
Printf ("% d", root-> data );
Free (root );
}
}
Void test ()
{
TNode root = NULL;
Root = createTree (root );
PreorderTraverse (root );
Printf ("\ n ");
Int depth = treeDepth (root );
Printf ("depth = % d \ n", depth );
TreeFree (root );
}
Int main ()
{
Test ();
Return 0;
}
/*****************************\
1
2 6
-1 5 4-1
-1-1-1-1
\*****************************/