先序遍历:中、左、右
中序遍历:左、中、右
后序遍历:左、右、中
比如下面这科树
1
2 3
4 5 6 7
package com.sangfor.tree; public class Node { public int value; public Node left; public Node right; public Node(int value) { this.value = value; }
}
package com.sangfor.tree;import java.util.Stack;public class ForEachTree {public static void main(String[] args) {Node node1 = new Node(1);Node node2 = new Node(2);Node node3 = new Node(3);Node node4 = new Node(4);Node node5 = new Node(5);Node node6 = new Node(6);Node node7 = new Node(7);node1.left = node2;node1.right = node3;node2.left = node4;node2.right =