【遍历二叉树】01二叉树的前序遍历【Binary Tree Preorder Traversal】
2014-04-08 16:15
134 查看
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树,返回他的前序遍历的节点的values。
例如:
给定一个二叉树
返回
笔记:
递归解决方案是微不足道的,你可以用迭代的方法吗?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
1.递归实现
test.cpp:
输出结果:
8 6 9 2 4 7 1
2.非递归实现(迭代实现)
根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:
对于任一结点P:
1)访问结点P,并将结点P入栈;
2)判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,循环至1);若不为空,则将P的左孩子置为当前的结点P;
3)直到P为NULL并且栈为空,则遍历结束。
test.cpp:
输出结果:
8 6 9 2 4 7 1
BinaryTree.h:
BinaryTree.cpp:
给定一个二叉树,返回他的前序遍历的节点的values。
例如:
给定一个二叉树
{1,#,2,3},
1 \ 2 / 3
返回
[1,2,3].
笔记:
递归解决方案是微不足道的,你可以用迭代的方法吗?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.递归实现
test.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; void preorder(TreeNode *root, vector<int> &path) { if(root != NULL) { path.push_back(root->val); preorder(root->left, path); preorder(root->right, path); } } vector<int> preorderTraversal(TreeNode *root) { vector<int> path; preorder(root, path); return path; } // 树中结点含有分叉, // 8 // / \ // 6 1 // / \ // 9 2 // / \ // 4 7 int main() { TreeNode *pNodeA1 = CreateBinaryTreeNode(8); TreeNode *pNodeA2 = CreateBinaryTreeNode(6); TreeNode *pNodeA3 = CreateBinaryTreeNode(1); TreeNode *pNodeA4 = CreateBinaryTreeNode(9); TreeNode *pNodeA5 = CreateBinaryTreeNode(2); TreeNode *pNodeA6 = CreateBinaryTreeNode(4); TreeNode *pNodeA7 = CreateBinaryTreeNode(7); ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5); ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7); PrintTree(pNodeA1); vector<int> ans = preorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } cout << endl; DestroyTree(pNodeA1); return 0; } |
8 6 9 2 4 7 1
2.非递归实现(迭代实现)
根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:
对于任一结点P:
1)访问结点P,并将结点P入栈;
2)判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,循环至1);若不为空,则将P的左孩子置为当前的结点P;
3)直到P为NULL并且栈为空,则遍历结束。
test.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; //非递归前序遍历 vector<int> preorderTraversal(TreeNode *root) { stack<TreeNode *> s; vector<int> path; TreeNode *p = root; while(p != NULL || !s.empty()) { while(p != NULL) { path.push_back(p->val); s.push(p); p = p->left; } if(!s.empty()) { p = s.top(); s.pop(); p = p->right; } } return path; } // 树中结点含有分叉, // 8 // / \ // 6 1 // / \ // 9 2 // / \ // 4 7 int main() { TreeNode *pNodeA1 = CreateBinaryTreeNode(8); TreeNode *pNodeA2 = CreateBinaryTreeNode(6); TreeNode *pNodeA3 = CreateBinaryTreeNode(1); TreeNode *pNodeA4 = CreateBinaryTreeNode(9); TreeNode *pNodeA5 = CreateBinaryTreeNode(2); TreeNode *pNodeA6 = CreateBinaryTreeNode(4); TreeNode *pNodeA7 = CreateBinaryTreeNode(7); ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5); ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7); PrintTree(pNodeA1); vector<int> ans = preorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } cout << endl; DestroyTree(pNodeA1); return 0; } |
8 6 9 2 4 7 1
BinaryTree.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #ifndef _BINARY_TREE_H_ #define _BINARY_TREE_H_ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode *CreateBinaryTreeNode(int value); void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight); void PrintTreeNode(TreeNode *pNode); void PrintTree(TreeNode *pRoot); void DestroyTree(TreeNode *pRoot); #endif /*_BINARY_TREE_H_*/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <iostream> #include <cstdio> #include "BinaryTree.h" using namespace std; /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //创建结点 TreeNode *CreateBinaryTreeNode(int value) { TreeNode *pNode = new TreeNode(value); return pNode; } //连接结点 void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight) { if(pParent != NULL) { pParent->left = pLeft; pParent->right = pRight; } } //打印节点内容以及左右子结点内容 void PrintTreeNode(TreeNode *pNode) { if(pNode != NULL) { printf("value of this node is: %d\n", pNode->val); if(pNode->left != NULL) printf("value of its left child is: %d.\n", pNode->left->val); else printf("left child is null.\n"); if(pNode->right != NULL) printf("value of its right child is: %d.\n", pNode->right->val); else printf("right child is null.\n"); } else { printf("this node is null.\n"); } printf("\n"); } //前序遍历递归方法打印结点内容 void PrintTree(TreeNode *pRoot) { PrintTreeNode(pRoot); if(pRoot != NULL) { if(pRoot->left != NULL) PrintTree(pRoot->left); if(pRoot->right != NULL) PrintTree(pRoot->right); } } void DestroyTree(TreeNode *pRoot) { if(pRoot != NULL) { TreeNode *pLeft = pRoot->left; TreeNode *pRight = pRoot->right; delete pRoot; pRoot = NULL; DestroyTree(pLeft); DestroyTree(pRight); } } |
相关文章推荐
- LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
- 144. Binary Tree Preorder Traversal 二叉树的前序遍历
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- lintcode 容易题:Binary Tree Preorder Traversal 二叉树的前序遍历
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
- 二叉树前序遍历Binary Tree Preorder Traversal (Java)
- 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- 二叉树遍历144. Binary Tree Preorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 前序和中序重建二叉树
- leetcode--第5章--树-5.1二叉树的遍历--5.1.1 binary tree preorder traversal
- Leetcode Binary Tree Preorder Traversal 二叉树先序遍历