劍指 二叉樹的下乙個節點

2021-09-17 02:42:28 字數 530 閱讀 7583

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

中序遍歷:左根右。給定節點是子樹的根節點,該節點的左子樹不用考慮,已經遍歷過了,只需要考慮其右子樹和父節點

1.給定節點的右子樹不為空,返回右子樹的最左節點;

2.給定節點的右子樹為空,會出現兩種情況:

public class treelinknode 

}

public class solution 

if(pnode.right != null)

return temp;

}else if(pnode.next!=null && pnode == pnode.next.left) else if(pnode.next!=null && pnode == pnode.next.right)

return temp.next;

}else

}}

劍指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...

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

主要要考慮到 當該節點沒有右子樹時,然後要判斷其為父節點的左子樹還是右子樹,同時特別要注意!邊界條件,當該節點沒有父節點和右子樹時的判斷 當然也要判斷該節點是否為null 如下 public class treelinknode public class solution else if pnode...