劍指offer 二叉樹的下乙個結點

2021-09-26 04:01:34 字數 625 閱讀 5209

給定乙個二叉樹和其中的乙個結點,請找出中序遍歷順序的下乙個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。

struct treelinknode 

};treelinknode* getnext(treelinknode* pnode){}

題目的意思是 next 指的該結點的父節點,相當於 parent 指標,按照以往的題套路應該是給這棵樹的根節點,以及想要查詢的結點資訊,但是此題,它沒有,根節點需要我們進行迴圈遍歷得到,pnode就是想要查詢的結點資訊。

由於是中序遍歷,根據中序遍歷規則可以得出

1)當pnode是父節點的左孩子時,返回 pnode ->next 即父節點

2)當 pnode 是父節點的右孩子時,返回右孩子的最左結點,否則迴圈向上遍歷,重複判斷。

3)其他情況返回null

treelinknode* getnext(treelinknode* pnode)

return pnode;

}while(pnode->next != null)

pnode = pnode->next;

}return null;

}

劍指offer 二叉樹的下乙個結點

題目描述 給定乙個二叉樹和其中的乙個結點,請找出中序遍歷順序的下乙個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。using namespace std struct treelinknode class solution treelinknode nextnode n...

劍指offer 二叉樹的下乙個節點

給定乙個二叉樹和其中的乙個結點,請找出中序遍歷順序的下乙個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。在編寫程式之前,先縷清思路。在該題總,應該分不同情況對其進行討論。情況一 魯棒性 目標節點為空節點時返回ptr 情況二 目標節點沒有父節點且沒有右子樹時,即該節點就是...

劍指offer 二叉樹的下乙個節點

struct treelinknode class solution return currnode case two the node does not has right son,it is the left son of its father if pnode next null return...