5.1.1—二叉树的遍历—Binary Tree Preorder Traversal

来源:互联网 发布:现在开童装淘宝店 编辑:程序博客网 时间:2024/06/08 00:20
描述
Given a binary tree, return the preorder traversal of its nodes’ values.
For example: Given binary tree {1,#,2,3},
1
 \
 2
/
3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?


#pragma once#include<iostream>struct BinaryTreeNode{int                    m_nValue;BinaryTreeNode*        m_pLeft;BinaryTreeNode*        m_pRight;};BinaryTreeNode* CreateBinaryTreeNode(int value);void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);void PrintTreeNode(BinaryTreeNode* pNode);void PrintTree(BinaryTreeNode* pRoot);void DestroyTree(BinaryTreeNode* pRoot);

#include "BinaryTree.h"BinaryTreeNode* CreateBinaryTreeNode(int value){BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight){if (pParent != NULL){pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}void PrintTreeNode(BinaryTreeNode* pNode){if (pNode != NULL){printf("value of this node is: %d\n", pNode->m_nValue);if (pNode->m_pLeft != NULL)printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);elseprintf("left child is null.\n");if (pNode->m_pRight != NULL)printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);elseprintf("right child is null.\n");}else{printf("this node is null.\n");}printf("\n");}void PrintTree(BinaryTreeNode* pRoot){PrintTreeNode(pRoot); if (pRoot != NULL){if (pRoot->m_pLeft != NULL)PrintTree(pRoot->m_pLeft);if (pRoot->m_pRight != NULL)PrintTree(pRoot->m_pRight);}}void DestroyTree(BinaryTreeNode* pRoot){if (pRoot != NULL){BinaryTreeNode* pLeft = pRoot->m_pLeft;BinaryTreeNode* pRight = pRoot->m_pRight;delete pRoot;pRoot = NULL;DestroyTree(pLeft);DestroyTree(pRight);}}


#include "BinaryTree.h"#include <stack>#include<vector>using namespace std;//===二叉树的前序遍历,递归版本void PreorderTraversal(BinaryTreeNode *proot){if (proot){cout << proot->m_nValue << " ";if (proot->m_pLeft)PreorderTraversal(proot->m_pLeft);if (proot->m_pRight)PreorderTraversal(proot->m_pRight);}}//===二叉树的前序遍历,迭代版本void PreorderTraversal2(BinaryTreeNode *proot){stack<BinaryTreeNode*> temp;vector<int> cahe;temp.push(proot);while (!temp.empty()){BinaryTreeNode *top = temp.top();temp.pop();if (top){cahe.push_back(top->m_nValue);temp.push(top->m_pRight);temp.push(top->m_pLeft);}}//======for (int i = 0; i < cahe.size(); i++)cout << cahe[i] << " ";cout << endl;}// ====================测试代码====================//            8//        6      10//       5 7    9  11int main(){BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);ConnectTreeNodes(pNode8, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);//===//PrintTree(pNode8);//===PreorderTraversal(pNode8);cout << endl;//===PreorderTraversal2(pNode8);//==DestroyTree(pNode8);}



阅读全文
0 0
原创粉丝点击