Leetcode/Top 100 Liked

[Binary Tree][Easy] 94. Binary Tree Inorder Traversal

자전거통학 2024. 3. 8. 23:00

https://leetcode.com/problems/binary-tree-inorder-traversal/description

 

Q. 주어진 tree를 inorder로 순회하고 그 값을 반환하라. 

 

 

Solution. 

  Binary Tree의 기본기. 

  가급적 풀 줄 알아야 하겠다. 

 

void inorderTraverseBT(vector<int>& vOut, TreeNode* node)
{
    if (node == NULL)	return;

    inorderTraverseBT(vOut, node->left);
    vOut.push_back(node->val);
    inorderTraverseBT(vOut, node->right);
}

vector<int> inorderTraversal(TreeNode* root) 
{
    vector<int> vRet;
    inorderTraverseBT(vRet, root);
    return vRet;
}

 

결과.