LeetCode OJ - Binary Tree Preorder Traversal
2014-05-11 20:57
239 查看
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?
递归代码
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?
递归代码
/** * Definition for binary tree * 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> rslt; preorder(root, rslt); return rslt; } void preorder(TreeNode *root, vector<int> &rslt) { if(root) { rslt.push_back(root->val); preorder(root->left, rslt); preorder(root->right, rslt); } } };
相关文章推荐
- LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode OJ】Binary Tree Preorder Traversal
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ - Binary Tree Preorder Traversal
- LeetCode OJ 之 Binary Tree Preorder Traversal (二叉树的前序遍历)
- LeetCode OJ:Binary Tree Preorder Traversal
- [LeetCode OJ] Binary Tree Preorder Traversal 解题报告
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)
- leetcode OJ -Binary Tree Preorder Traversal(2014.1.20)
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal
- 【LEETCODE OJ】Binary Tree Preorder Traversal
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ--Binary Tree Preorder Traversal
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode OJ]Binary Tree Preorder Traversal
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal