problem
solution
option 1 - inorder traverse
按照inorder 拜訪每一節點,並存到vector,拜訪完,檢查vector是否為單調遞增陣列即可。
option 2 - dfs
拜訪每一個節點,並檢查該節點的值介於左右孩子的值
1 | class Solution { |
option 3
請參考 morris traversal
可以做到 O(1)
space
1 | class Solution { |
analysis
- optnio 1
- time complexity
O(n)
- space complexity
O(n)
- time complexity
- option 2
- time complexity
O(n)
- space complexity
O(h)
since recursive inorder traversal will lead to stack frame creation equal to the height of the tree.
- time complexity