首先我們定義的二元查找樹 節點的資料結構如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
4.在二元樹中找出和為某一值的所有路徑(樹)
題目:輸入一個整數和一棵二元樹。
從樹的根結點開始往下訪問一直到葉結點所經過的所有結點形成一條路徑。
列印出和與輸入整數相等的所有路徑。
例如 輸入整數22和如下二元樹
10
/ /
5 12
/ \
4 7
則列印出兩條路徑:10, 12和10, 5, 7。
二元樹節點的資料結構定義為:
struct BinaryTreeNode // a node in the binary tree
{
int m_nValue; // value of node
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node
};
52.二元樹的深度(樹)。
題目:輸入一棵二元樹的根結點,求該樹的深度。
從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。
例如:輸入二元樹:
10
/ /
6 14
/ / /
4 12 16
輸出該樹的深度3。
二元樹的結點定義如下:
struct SBinaryTreeNode // a node of the binary tree
{
int m_nValue; // value of node
SBinaryTreeNode *m_pLeft; // left child of node
SBinaryTreeNode *m_pRight; // right child of node
};
分析:這道題本質上還是考查二元樹的遍歷。
63.在字串中刪除特定的字元(字串)。
題目:輸入兩個字串,從第一字串中刪除第二個字串中所有的字元。
例如,輸入”They are students.”和”aeiou”,
則刪除之後的第一個字串變成”Thy r stdnts.”。
分析:這是一道微軟面試題。在微軟的常見面試題中,與字串相關的題目占了很大的一部分,
因為寫程式操作字串能很好的反映我們的程式設計基本功。