Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
要求中序遍历二叉树,非递归形式,这里总结的比较全面,各种遍历方式都有,点我
我写的是递归调用,非递归没太看懂
/**
* 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:
void helper(TreeNode* p, vector<int> &res){
if(p==NULL) return;
helper(p->left, res);
res.push_back(p->val);
helper(p->right, res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
helper(root, res);
return res;
}
};