您的位置:首页 > 其它

Binary Tree Inorder Traversal

2015-07-10 16:41 253 查看
https://leetcode.com/problems/binary-tree-inorder-traversal/

递归中序遍历

/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> res;
inorderVisit(root,res);
return res;

}
void inorderVisit(TreeNode * temp,vector<int>& res)
{
if(temp==NULL)
return;
if(temp->left!=NULL)
inorderVisit(temp->left,res);
res.push_back(temp->val);
if(temp->right!=NULL)
inorderVisit(temp->right,res);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: