原題:
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree
Screen Shot 2017-11-05 at 4.49.41 PM.png
Screen Shot 2017-11-05 at 4.51.37 PM.png
所犯的錯誤:
開始21,28行沒有做賦值給root,導致出錯。root是作為對象傳入給函數的,如果不將返回值賦值給root,那么原來的Root等于沒有任何函數操作所帶來的改變。
Screen Shot 2017-11-05 at 4.53.16 PM.png
開始第18,22都沒有寫elif ,只是寫了if。由于root可能在上一個if語句中發生變化,導致符合下一個If的條件,又進入了下一個if,這樣就發生了錯誤。必須采用elif將他們互斥。
開始沒有寫28,21行,而在函數最后的return中這樣寫:return self.trimBST(root,L,R),導致程序進入無限循環而溢出棧。