Beispiel #1
0
        public static void TreeTraversal()
        {
            //Define a binary tree.
            //            1
            //          /   \
            //         2     3
            //        / \   / \
            //       4  5   6  7 

            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.right = new TreeNode(3);
            root.left.left = new TreeNode(4);
            root.left.right = new TreeNode(5);
            root.right.left = new TreeNode(6);
            root.right.right = new TreeNode(7);

            List<string> TreePath = root.ListBinaryTreePath1(root);
            List<string> TreePath2 = root.ListBinaryTreePath2(root);

            List<int> preResult = root.PreOrderTraversal(root);     //1, 2, 4, 5, 3, 6, 7
            List<int> preResult_i = root.PreOrderTraversal_Iterative1(root);
            List<int> preResult_i2 = root.PreOrderTraversal_Iterative2(root);

            List<int> inResult = root.InOrderTraversal(root);       //4, 2, 5, 1, 6, 3, 7
            List<int> inResult_i = root.InOrderTraversal_Iterative1(root); 

            List<int> postResult = root.PostOrderTraversal(root);   //4, 5, 2, 6, 7, 3, 1
            List<int> postResult_i = root.PostOrderTraversal_Iterative1(root);
            List<int> postResult_i2 = root.PostOrderTraversal_Iterative2(root);

            List<List<int>> levelResult = root.LevelOrderTraversal(root);
        }