Leetcode 94. 二叉树的中序遍历 C++
程序员文章站
2022-05-20 13:49:37
...
Leetcode 94. 二叉树的中序遍历
题目
给定一个二叉树,返回它的中序 遍历。
测试样例
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
题解
递归实现中序遍历
代码
vector<int> ans;
void search(TreeNode* root){
if(root->left){
search(root->left);
}
ans.push_back(root->val);
if(root->right){
search(root->right);
}
}
vector<int> inorderTraversal(TreeNode* root) {
if(root == NULL) return ans;
search(root);
return ans;
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
上一篇: LeetCode 145二叉树的后序遍历
下一篇: 栈---二叉搜索树迭代器
推荐阅读
-
Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
-
Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
-
Python利用前序和中序遍历结果重建二叉树的方法
-
Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
-
[PHP] 算法-根据前序和中序遍历结果重建二叉树的PHP实现
-
【算法】二叉树的前序、中序、后序、层序遍历和还原。
-
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
-
C++实现LeetCode(105.由先序和中序遍历建立二叉树)
-
C++实现LeetCode(889.由先序和后序遍历建立二叉树)
-
C++实现LeetCode(106.由中序和后序遍历建立二叉树)