Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 738 Bytes

0094._binary_tree_inorder_traversal.md

File metadata and controls

46 lines (37 loc) · 738 Bytes

94. Binary Tree Inorder Traversal

难度: Medium

刷题内容

原题连接

内容描述

给定一个二叉树,返回它的中序 遍历。

示例:

输入: [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;
}