1typedefstructnode{2 intVal;3 structNode *left, *Right ;4 }treenode;5 //find the number of nodes in the binary tree. 6 intGetnodenum (TreeNode *proot) {7 if(Proot = =NULL)8 return 0;9 returnGetnodenum (Proot->left) + getnodenum (proot->right) +;Ten } One //finding the depth of a binary tree A intGetdepth (treenode*root) { - if(Root = =NULL) - return 0; the intDepth_left = Getdepth (root->Left ); - intDepth_right = Getdepth (root->Right ); - returnMax (Depth_right, Depth_left) +1; - } + //three ways to traverse - voidPreordertraverse (TreeNode *root) { + if(Root = =NULL) A return; atVisit (root->val); -Preordertraverse (root->Left ); -Preordertraverse (root->Right ); - } - voidInordertraverse (TreeNode *root) { - if(Root = =NULL) in return; -Inordertraverse (root->Left ); toVisit (root->val); +Inordertraverse (root->Right ); - } the voidPostordertraverse (TreeNode *root) { * if(Root = =NULL) $ return;Panax NotoginsengPostordertraverse (root->Left ); -Postordertraverse (root->Right ); theVisit (root->val); + } A //sequence Traversal binary tree the voidLeveltraverse (TreeNode *root) { + if(Root = =NULL) - return; $Queue<treenode*>Q; $ Q.push (root); - while(!Q.empty ()) { -TreeNode *node =Q.front (); the Q.pop (); -Visit (root->val);Wuyi if(Node->left! =NULL) theQ.push (node->Left ); - if(Node->right! =NULL) WuQ.push (node->Right ); - } About return; $ } - //find the number of nodes in the K-layer of the binary tree - intGetnodenumkthlevel (TreeNode *root,intk) { - if(Root = NULL | | K <1) A return 0; + if(k = =1) the return 1; - intNumleft = Getnodenumkthlevel (Root->left, K-1); $ intNumright = Getnodenumkthlevel (Root->right, K-1); the return(Numleft +numright); the } the //finding the number of leaf nodes in binary tree the intGetleafnodenum (TreeNode *root) { - if(Root = =NULL) in return 0; the if(Root->left = = NULL && Root->right = =NULL) the return 1; About intNumleft = Getleafnodenum (root->Left ); the intNumright = Getleafnodenum (root->Right ); the return(Numleft +numright); the + } - //determine if two binary trees are of the same structure the BOOLIssame (TreeNode *tree1, TreeNode *tree2) {Bayi if(Tree1 = = NULL && Tree2 = =NULL) the return true; the if(tree1 = = NULL && tree2! = NULL | | Tree1! = NULL && Tree2 = = NULL | | -Tree1->val! = tree2->val) - return false; the returnIssame (Tree1->left, Tree2->left) && issame (Tree1->right, tree2->Right ); the } the //determine if a binary tree is a balanced binary tree the BOOLIsavl (TreeNode *root,int&height) { - if(Root = =NULL) { theHeight =0; the return true; the }94 intHeightleft; the BOOLResultleft = Isavl (root->Left , heightleft); the intHeightright; the BOOLResultright;98 BOOLResultright = Isavl (root->Right , heightright); AboutHeight = max (heightleft, heightright) +1; - if(Resultleft && resultright && (ABS (heightleft-heightright) <=1)){101 return true;102}Else {103 return false;104 } the }106 //to find the image of a binary tree107 voidMirror (TreeNode *root) {108 if(Root = =NULL)109 return; the if(Root->left = = NULL && Root->right = =NULL)111 return; theTreeNode *temp;113temp = root->Left ; theRoot->left = root->Right ; theRoot->right =temp; theMirror (root->Left );117Mirror (root->Right );118 }119 //finding the lowest common ancestor node of two nodes in a binary tree - 121 //finding the maximum distance of a node in a binary tree122 //reconstruction of Binary tree by pre-sequence traversal and middle sequence traversal123 //Judging the binary tree is not exactly a binary tree124 //Turn a two-fork lookup tree into an ordered doubly linked list
A common algorithm for binary tree