-
题目链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
-
思路:按照左节点 -> 根节点 -> 右节点的顺序遍历
-
代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> ans; vector<int> inorderTraversal(TreeNode* root) { if(root){ inorderTraversal(root -> left); ans.push_back(root -> val); inorderTraversal(root -> right); } return ans; } };
94.二叉树的中序遍历
最新推荐文章于 2024-08-23 11:46:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)