94. Binary Tree Inorder Traversal

1. iterative

若 cur 不為 null

一路走左, 更新 cur 為左

到底了 => pop(), 塞值,

檢查右邊有無元素

有: 又一路走左

無: pop() 塞值

反覆...

O(n), O(n)

or like this

只有外面有 while, 好像比較合理, 概念一樣

2. recursion

時間複雜度:O (n )。

空間複雜度:最壞情況下需要空間上O (n ),平均情況為O( log n)。

this is more clean

Last updated

Was this helpful?