Leetcode:94.二叉树的中序遍历

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

示例:

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

输出: [1,3,2]

解题思路:

一般的数据结构书上都有,递归实现较为容易。

                                  

C++代码
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        DFS(root);
        return res;
    }
private:
    void DFS(TreeNode* root) {
        if (root == NULL) {  return; }
        if (root->left != NULL) DFS(root->left);
        res.push_back(root->val);
        if (root->right != NULL) DFS(root->right);
    }
private:
    vector<int> res;
};

猜你喜欢

转载自blog.csdn.net/qq_23523409/article/details/83716092