Number of leaf nodes
/*
(1) If the binary tree is empty, return 0
(2) If the binary tree is not empty and the left and right sub-tree is empty, return 1
(3) If the binary tree is not empty, and the left and right subtrees are not empty at the same time, return the number of leaves in the left subtree plus the number of leaf nodes in the subtree.
*/
int Getleafnodenum (btree* root) {if (root = null) return 0;if (Root->m_pleft = = NULL && Root->m_pright = NULL ) return 1;int Leafnumofleft = Getleafnodenum (root->m_pleft); int leafnumofright = Getleafnodenum (root->m_pRight) ; int ret = leafnumofleft + leafnumofright;return ret;}
/*
Determine whether the structure of a binary tree is the same
1: Returns True if two two-fork trees are empty
2: If a binary tree is empty and the other one is not empty, then return false
3: If two two-fork trees are not empty, return true if they have the same result as the Saozi right subtree, or false
*/
BOOL IsEqual (btree* root1, btree* root2) {if (ROOT1 = = NULL && ROOT2 = null) return True;else if ((ROOT1 = = NULL && root2!= NULL) | | (ROOT1 = null && ROOT2 = = null)) return False;bool Equalleft = isequal (root1->m_pleft,root2->m_pleft); bool Equalright = IsEqual (root1->m_ Pright,root2->m_pright); return (Equalleft && equalright);}
Complete test Code:
BTNumOfKLevel.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;class btree{public:int m_nvalue; Btree* M_pleft; Btree* M_pright; BTree (int m): M_nvalue (m) {m_pleft = M_pright = null;}};/ /Two insert implementation of the fork Tree void Insert (int value, btree* &root) {if (root = NULL) {root = new BTree (value);} else if (value < Root->m_nvalue) Insert (value,root->m_pleft), else if (value > Root->m_nvalue) Insert ( Value,root->m_pright); else;} Number of leaf nodes/* (1) If the binary tree is empty, return 0 (2) If the binary tree is not empty and the left and right subtree is empty, return 1 (3) If the binary tree is not empty, and the left and left subtree are not empty, the number of leaf nodes in the subtree and the number of leaf nodes in */int Getleafnodenum (btree* root) {if (root = null) return 0;if (Root->m_pleft = = NULL && root->m_pright = null) return 1;int Leafnumofleft = Getleafnodenum (root->m_pleft); int leafnumofright = Getleafnodenum (root->m_pRight); int ret = leafnumofleft + leafnumofright;return ret;} /* Determine if the structure of a binary tree is the same 1: If two two forks are empty, then return true2: If a binary tree is empty and the other one is not empty, then return FALSE3: If two two forks are not empty, then if the results of their Saozi right subtree are the same, Returns true otherwise returns False*/bool isequal (btree* root1, Btree* Root2) {if (ROOT1 = = NULL && Root2 = = null) return True;else if ((ROOT1 = = null && root2!= null) | | (ROOT1 = null && ROOT2 = = null)) return False;bool Equalleft = isequal (root1->m_pleft,root2->m_pleft); bool Equalright = IsEqual (root1->m_ Pright,root2->m_pright); return (Equalleft && equalright);} int _tmain (int argc, _tchar* argv[]) {btree* m_proot = new BTree (4); insert (3,m_proot); insert (6,m_proot); Insert (1,m_ Proot); insert (2,m_proot); insert (5,m_proot); insert (8,m_proot); insert (7,m_proot); insert (10,m_proot); btree* M_proot2 = new BTree (4), insert (3,m_proot2), insert (6,m_proot2), insert (1,m_proot2), insert (2,m_proot2), insert ( 5,M_PROOT2); insert (8,m_proot2); insert (7,m_proot2); insert (10,m_proot2); int count = Getleafnodenum (m_proot); cout << "The number of leaf nodes is:" <<count<<endl;cout<< "two tree structure is the same:" <<isequal (M_PROOT,M_PROOT2); GetChar (); return 0;}
C + + algorithm for finding the number of leaf nodes in binary tree and judging whether two binary trees have the same structure