problem
solution
option 1 - STL to store
1 | class Solution { |
option 2 - recursive + preorder + two Pointers
因為只會有兩個節點放錯位置且inorder遍歷應該是有序的,故先找到哪兩個節點在交換
1 | class Solution { |
option 3 - stack + preorder
1 | class Solution { |
option 4 - Morris traverse
analysis
- option 1
- time complexity
O(n)
- space complexity
O(n)
- time complexity
- option 2
- time complexity
O(n)
- space complexity
O(n)
- time complexity
- option 3
- time complexity
O(n)
- space complexity
O(1)
- time complexity