(python的坑,坑的我頭暈,下行迴圈寫後根遍歷)
總是提示越界錯誤
IndexError: list index out of range
### if s and s[-1].left == t:
需要先判斷s是否為空,假如:if s[-1].left == t and s就會出現越界。
## 邊界條件的非常非常重要!!!!
## 程式碼如下:
```python
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
s = []
t = root
temp =t.item
while t is not None or s:
while t is not None:
s.append(t)
t = t.left if t.left is not None else t.right
t = s.pop()
ans += [t.item]
print ans
if s and s[-1].left == t:
t = s[-1].right
else:
t = None
return ans