If you don’t need recursion, then use depth first! Use the stack, first push the root node into the stack, if the stack is not empty, then pop it out and output the median value of the current node, then push the right subtree onto the stack first, then push the left subtree onto the stack, and then Determine whether the stack is empty, loop... The steps are as follows: 1) First push the root node of the binary tree into the stack 2) Determine whether the stack is empty, if not, pop it out of the stack and output the pop tree The value of the node 3) The right subtree of the pop tree node is pushed onto the stack 4) The left subtree of the pop tree node is pushed onto the stack 5) Loop back to (2) This is the one I saw before Method, I wonder if it can help the questioner?
If you don’t need recursion, then use depth first!
Use the stack, first push the root node into the stack, if the stack is not empty, then pop it out and output the median value of the current node, then push the right subtree onto the stack first, then push the left subtree onto the stack, and then Determine whether the stack is empty, loop... The steps are as follows:
1) First push the root node of the binary tree into the stack
2) Determine whether the stack is empty, if not, pop it out of the stack and output the pop tree The value of the node
3) The right subtree of the pop tree node is pushed onto the stack
4) The left subtree of the pop tree node is pushed onto the stack
5) Loop back to (2)
This is the one I saw before Method, I wonder if it can help the questioner?
Replace recursion with stack: https://zh.coursera.org/learn...
Depth first? . .
Use breadth-first traversal, then store all the parent nodes of the node in the state, and output them after reaching the leaf node.