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?
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
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- leetcode--第5章--树-5.1二叉树的遍历--5.1.1 binary tree preorder traversal
- 二叉树的先序遍历 Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal--二叉树的先序遍历
- Binary Tree Preorder Traversal 二叉树的前序遍历
- binary-tree-preorder-traversal(二叉树的前序遍历)
- LeetCode144——Binary Tree Preorder Traversal二叉树先序遍历
- 二叉树先序遍历——144. Binary Tree Preorder Traversal
- 【二叉树先序遍历】Binary Tree Preorder Traversal
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
- binary-tree-preorder-traversal(先序遍历二叉树)
- 二叉树遍历144. Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal 二叉树的先序遍历,使用堆栈,非递归
- LeetCode OJ 之 Binary Tree Preorder Traversal (二叉树的前序遍历)
- LintCode Binary Tree Preorder Traversal二叉树的前序遍历(非递归)
- Binary Tree Preorder Traversal 二叉树的前向遍历(迭代非递归版本)
- 66.Binary Tree Preorder Traversal-二叉树的前序遍历(容易题)
- 144. Binary Tree Preorder Traversal (二叉树的先序遍历)
- docker
- select和epoll的区别
- 4.1.4—栈—Evaluate Reverse Polish Notation
- impala保存时间类型timestamp---NanoTimeUtils根据JdateTime生成nanotime
- xshell5 的账号密码搬家
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- STM32F407单片机SDIO接口卡在SDIO_STA_CMDACT上无法发送命令的解决方法
- 邻接表 的 DFS 与 BFS
- JavaScript调用UEditor相册模块
- sqlServer自定义函数
- 【数论-莫比乌斯】hdu 2841 Visible Trees
- 5.1.2—二叉树的遍历—Binary Tree Inorder Traversal
- 上楼梯
- TF随笔-13