Binary Tree Traversal(Preorder, Inorder, Postorder )
2016-09-19 10:41
423 查看
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?
144. Binary Tree Preorder Traversal
94. Binary Tree Inorder Traversal
145. Binary Tree Postorder Traversal
递归:
迭代:
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?
144. Binary Tree Preorder Traversal
94. Binary Tree Inorder Traversal
145. Binary Tree Postorder Traversal
递归:
//Recursive preorder Runtime: 3 ms /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL){ return res; } if(root){ res.push_back(root->val); } if(root->left){ vector<int> leftv=preorderTraversal(root->left); res.insert(res.end(),leftv.begin(),leftv.end()); } if(root->right){ vector<int> rightv=preorderTraversal(root->right); res.insert(res.end(),rightv.begin(),rightv.end()); } return res; } };
//Recursive inorder Runtime: 0 ms class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL){ return res; } if(root->left){ vector<int> leftv=inorderTraversal(root->left); res.insert(res.end(),leftv.begin(),leftv.end()); } res.push_back(root->val); if(root->right){ vector<int> rightv=inorderTraversal(root->right); res.insert(res.end(),rightv.begin(),rightv.end()); } return res; } };
//Recursive postorder Runtime: 0 ms class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL){ return res; } if(root->left){ vector<int> leftv=postorderTraversal(root->left); res.insert(res.end(),leftv.begin(),leftv.end()); } if(root->right){ vector<int> rightv=postorderTraversal(root->right); res.insert(res.end(),rightv.begin(),rightv.end()); } res.push_back(root->val); return res; } }
迭代:
//iterative preorder Runtime: 0 ms class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> prestack; while(root||!prestack.empty()){ while(root){ prestack.push(root); res.push_back(root->val); root=root->left; } root=prestack.top(); prestack.pop(); root=root->right; } return res; } };
// iterative inorder Runtime: 3 ms class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> prestack; while(root||!prestack.empty()){ while(root){ prestack.push(root); root=root->left; } root=prestack.top(); res.push_back(root->val); prestack.pop(); root=root->right; } return res; } };
//interative postorder Runtime: 0 ms class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> prestack; while(root||!prestack.empty()){ while(root){ prestack.push(root); res.insert(res.begin(),root->val); root=root->right; } root=prestack.top(); prestack.pop(); root=root->left; } return res; } };
相关文章推荐
- [LC 94 144 145]Binary Tree Traversal ( preorder, inorder,postorder)
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- [Leetcode] Binary tree travelsal (preorder, inorder, postorder)
- construct-binary-tree-from-preorder-and-inorder-traversal
- construct-binary-tree-from-inorder-and-postorder-traversal
- construct-binary-tree-from-inorder-and-postorder-traversal Java code
- preorder, inorder, postorder traversal
- construct-binary-tree-from-preorder-and-inorder-traversal
- LeetCode--Binary Tree Traversal ( Inorder, Preorder, PostOrder)
- 已知二叉树的先序遍历(preorder)和中序遍历(inorder) 或 中序和后序(postorder),还原该二叉树
- Preorder, Inorder, and Postorder tree Traversal Iterative solutions
- leetcode — construct-binary-tree-from-preorder-and-inorder-traversal
- construct-binary-tree-from-inorder-and-postorder-traversal
- construct-binary-tree-from-preorder-and-inorder-traversal Java code
- leetcode — construct-binary-tree-from-inorder-and-postorder-traversal
- construct-binary-tree-from-inorder-and-postorder-traversal
- LeetCode--construct-binary-tree-from-inorder-and-postorder-traversal
- 二叉树的先序遍历(preorder),中序遍历(inorder),后序遍历(postorder)
- construct-binary-tree-from-preorder-and-inorder-traversal
- LeetCode--construct-binary-tree-from-preorder-and-inorder-traversal