First-order traversal
public void Getnodebystack (TreeNode node) {//Stack implementation first-order traversal
stack<treenode> stack = new stack<> ();
if (node==null) {
Return
}
Stack.push (node);
while (!stack.isempty ()) {
TreeNode TreeNode = Stack.pop ();
System.out.print (treenode.vl+ "");
if (treenode.right!=null) {
Stack.push (Treenode.right);
}
if (treenode.left!=null) {
Stack.push (Treenode.left);
}
}
}
Directly pull the top node out of the stack, then put the node into the stack, and then put the left node
Second, the middle sequence traversal
public void Getnodebystack (TreeNode node) {//stack implementation in sequence traversal
stack<treenode> stack = new stack<> ();
if (node==null) {
Return
}
Stack.push (node);
while (!stack.isempty ()) {
TreeNode TreeNode = Stack.pop ();
if (Treenode.flag) {
if (treenode.right!=null) {
Stack.push (Treenode.right);
}
Treenode.flag = false;
Stack.push (TreeNode);//node to go back to stack after decomposition
if (treenode.left!=null) {
Stack.push (Treenode.left);
}
}else{
System.out.print (treenode.vl+ "");
}
}
}
The difference between the sequence traversal and the first order is to have the sign bit flag, because the node's first stack is decomposed into the left and right nodes, the second is to traverse the output