static class TreeNode { int val; TreeNode left; TreeNode right; int deep; } static void accessTree1 ( TreeNode root, List < Integer > res) { if ( root == null ) { return ; } res. add ( root. val) ; accessTree1 ( root. left, res) ; accessTree1 ( root. right, res) ; } static void accessTree2 ( TreeNode root, List < Integer > res) { if ( root == null ) { return ; } accessTree2 ( root. left, res) ; res. add ( root. val) ; accessTree2 ( root. right, res) ; } static void accessTree3 ( TreeNode root, List < Integer > res) { if ( root == null ) { return ; } accessTree3 ( root. left, res) ; accessTree3 ( root. right, res) ; res. add ( root. val) ; } public static void main ( String [ ] args) { TreeNode node1 = new TreeNode ( ) ; TreeNode node2 = new TreeNode ( ) ; TreeNode node3 = new TreeNode ( ) ; TreeNode node4 = new TreeNode ( ) ; TreeNode node5 = new TreeNode ( ) ; TreeNode node6 = new TreeNode ( ) ; TreeNode node7 = new TreeNode ( ) ; node1. val = 1 ; node2. val = 2 ; node3. val = 3 ; node4. val = 4 ; node5. val = 5 ; node6. val = 6 ; node7. val = 7 ; node1. left = node2; node2. left= node4; node2. right= node5; node1. right = node3; node3. left = node6; node6. left = node7; List < Integer > res = new ArrayList < > ( ) ; accessTree1 ( node1, res) ; System . out. println ( "先序遍历:" + res) ; res. clear ( ) ; accessTree2 ( node1, res) ; System . out. println ( "中序遍历:" + res) ; res. clear ( ) ; accessTree3 ( node1, res) ; System . out. println ( "后序遍历:" + res) ; res. clear ( ) ; }