Skip to content

94. Binary Tree Inorder Traversal

难度: Medium

刷题内容

原题连接

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

内容描述 ``` 给定一个二叉树,返回它的中序 遍历。

示例:

输入: [1,null,2,3] 1 \ 2 / 3

输出: [1,3,2]

进阶: 递归算法很简单,你可以通过迭代算法完成吗? ```

解题方案

思路 1

递归。
void dfs(TreeNode* root, vector<int>& ans){
    if(root==NULL)
        return ;
    dfs(root->left, ans);
    ans.push_back(root->val);
    dfs(root->right, ans);
}
vector<int> inorderTraversal(TreeNode* root) {
    vector<int> ans;
    dfs(root, ans);
    return ans;
}

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组