5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
2017-08-06 16:04
501 查看
描述
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); else printf("left child is null.\n"); if (pNode->m_pRight != NULL) printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue); else printf("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 11 int 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); }
相关文章推荐
- leetcode--第5章--树-5.1二叉树的遍历--5.1.1 binary tree preorder traversal
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- 【遍历二叉树】01二叉树的前序遍历【Binary Tree Preorder Traversal】
- 二叉树遍历144. Binary Tree Preorder Traversal
- lintcode 容易题:Binary Tree Preorder Traversal 二叉树的前序遍历
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
- 二叉树前序遍历Binary Tree Preorder Traversal (Java)
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal 二叉树的前序遍历
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
- LeetCode | Binary Tree Level Order Traversal(二叉树层序遍历)
- Binary Tree Level Order Traversal 二叉树按层遍历
- Binary Tree Postorder Traversal 二叉树的后序遍历,使用堆栈,非递归