二叉樹的基本操作,以及相關面試題(C 實現)

2021-08-25 17:27:00 字數 4741 閱讀 1869

【面試題】

1、建立二叉樹

#includeusing namespace std;

templatestruct bintreenode

bintreenode* _pleft;

bintreenode* _pright;

t _data;

};templateclass bintree

bintree(const t* array,size_t size,const t& invalid)

~bintree()

void createbintree(pnode& proot, const t* array, size_t size,size_t& index,const t& invalid) }

pnode copybintree(pnode proot)

return pnewnode;

} void destorybintree(pnode& proot) }

};

2、前/中/後遍歷二叉樹(遞迴&非遞迴)

//前序遍歷

void preorder(pnode proot) }

//非遞迴前序遍歷

void _preorder_nor()

cout << endl;

} //中序遍歷

void inorder(pnode proot) }

//非遞迴中序遍歷

void _inorder_nor()

pcur = s.top();

cout << pcur->_data << " ";

s.pop();

pcur = pcur->_pright;

}cout << endl;

} //後序遍歷

void endorder(pnode proot) }

//非遞迴後序遍歷

void _endorder_nor()

_proot = s.top();

if (null == _proot->_pright || _proot->_pright == ptmp)

else

} cout << endl;

}

3、層序遍歷二叉樹

void levelorder(pnode proot)

cout << endl;

}

4、求二叉樹的高度

size_t height(pnode proot)

5、求二叉樹中結點的個數

size_t size(pnode proot)

6、求葉子結點的個數

size_t getleaf(pnode proot)

7、求二叉樹中第k層結點的個數

size_t getklevelnode(pnode proot,size_t k)

8、判斷乙個節點是否在一棵二叉樹中

bool isnodeinbintree(pnode proot, pnode pnode)

9、判斷一棵二叉樹是否是完全二叉樹

bool iscomporebintree()

if (pcur->_pleft&&pcur->_pright)

else if (pcur->_pleft)

else if (pcur->_pright)

return false;

else

isflag = true;

q.pop();

} return true;

}

10、求二叉樹的映象 

void premarroy(pnode proot)		}

void inmarroy(pnode proot) }

void endmarroy(pnode proot) }

void feimarroy(pnode proot)

}

整體**:

#include#include#include#includeusing namespace std;

templatestruct bintreenode

bintreenode* _pleft;

bintreenode* _pright;

t _data;

};templateclass bintree

bintree(const t* array,size_t size,const t& invalid)

//bintree& operator=(const bintree& bt)

// // return *this;

//}~bintree()

//前序遍歷

void _preorder()

//非遞迴前序遍歷

void _preorder_nor()

cout << endl;

} //中序遍歷

void _inorder()

//非遞迴中序遍歷

void _inorder_nor()

pcur = s.top();

cout << pcur->_data << " ";

s.pop();

pcur = pcur->_pright;

}cout << endl;

} //後序遍歷

void _endorder()

//非遞迴後序遍歷

void _endorder_nor()

_proot = s.top();

if (null == _proot->_pright || _proot->_pright == ptmp)

else

} cout << endl;

} //層序遍歷

void _levelorder()

//求樹的高度

void _height()

//樹的節點個數

void _size()

//樹的葉子節點個數

void _getleaf()

//第k層節點個數

void _getklevelnode(size_t k)

//查詢結點是否存在

void _isnodeinbintree(pnode pnode)

//查詢結點

pnode _find(const t& data)

//判斷二叉樹是否為完全二叉樹

void _iscomporebintree()

//求二叉樹的映象

void _premarroy()

void _inmarroy()

void _endmarroy()

//非遞迴的方法求二叉樹的映象

void _feimarroy()

private:

void createbintree(pnode& proot, const t* array, size_t size,size_t& index,const t& invalid) }

pnode copybintree(pnode proot)

return pnewnode;

} void destorybintree(pnode& proot) }

void preorder(pnode proot) }

void inorder(pnode proot) }

void endorder(pnode proot) }

void levelorder(pnode proot)

cout << endl;

} size_t height(pnode proot)

size_t size(pnode proot)

size_t getleaf(pnode proot)

size_t getklevelnode(pnode proot,size_t k)

bool isnodeinbintree(pnode proot, pnode pnode)

pnode find(pnode proot, const t& data)

bool iscomporebintree()

if (pcur->_pleft&&pcur->_pright)

else if (pcur->_pleft)

else if (pcur->_pright)

return false;

else

isflag = true;

q.pop();

} return true;

} void premarroy(pnode proot) }

void inmarroy(pnode proot) }

void endmarroy(pnode proot) }

void feimarroy(pnode proot) }

private:

pnode _proot;

};void test()

int main()

測試結果截圖:

二叉樹相關面試題

void treepreorderbyloop treenode root return void treeinorderbyloop treenode root 若cur為空,取棧頂元素,訪問,出棧 treenode top null int ret gettop stack,top if ret...

二叉樹相關面試題

二叉樹的建立 1.求二叉樹節點個數 private static int count 0 public static void getsize node root count getsize root.left getsize root.right public static int getsize...

二叉樹的相關面試題

public class code01 dgmodel 先序遍歷 頭左右 public void preorderrecur node head system.out.println head.value preorderrecur head.left preorderrecur head.righ...