Leetcode Tree traverse problem
Given a binary tree, return the inorder, postorder and preorder traversal of its nodes’ values.
There are three solutions to the giving problem : 1) Recursive 2) Iterative 3) Morris
Morris solution is quite same for inorder and preorder but some kind of different for the postorder.
Inorder
Three ways to traverse the binary tree :
1) recursive time: O(N) space: O(N)
2) iterative time: O(N) space: O(N)
merge two while
3) morris traversal time: O(N) space: O(1)
|
|
Preorder
1) Recursive solution
2) Iterative solution
|
|
3) Morris traversal
晚点补充postorder的三种方法,先吃鸡!
1) etc
2) Iterative solution
Use a point node to avoid repeat traverse
3) Morris solution
A little bit tricky since we need use a dummy root to perform this algorithm.