++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树,返回他的中序遍历的节点的values。
例如:
给定一个二叉树 {1,#,2,3}
,
1 2 / 3
返回 [1,3,2].
笔记:
1 / 2 3 / 4 5
上面的二叉树可以序列化为"{1,2,3,#,#,4,#,#,5}"
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given a binary tree, return the inorder traversal of its nodes‘ values.
For
example:
Given binary tree {1,#,2,3}
,
1 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
confused
what "{1,#,2,3}"
means? >
read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where ‘#‘ signifies a path terminator where no node exists below.
Here‘s an example:
1 / 2 3 / 4 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 |
#include <iostream> #include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; void inorder(TreeNode *root, vector<int> &path) { if(root != NULL) { inorder(root->left, path); path.push_back(root->val); inorder(root->right, path); } } vector<int> inorderTraversal(TreeNode *root) { vector<int> path; inorder(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 = inorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } cout << endl; DestroyTree(pNodeA1); return 0; } |
2.非递归实现
根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:
对于任一结点P,
1)若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;
2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的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> inorderTraversal(TreeNode *root) { stack<TreeNode *> s; vector<int> path; TreeNode *p = root; while(p != NULL || !s.empty()) { while(p != NULL) { s.push(p); p = p->left; } if(!s.empty()) { p = s.top(); path.push_back(p->val); 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 = inorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } cout << endl; DestroyTree(pNodeA1); return 0; } |
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); } } |
【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】,布布扣,bubuko.com
【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】
原文:http://www.cnblogs.com/codemylife/p/3652261.html