题目链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/
题目描述
给定一个二叉树,返回它的中序 遍历。
示例
输入: [1,null,2,3] 1 2 / 3
输出: [1,3,2]
解决方法
采用递归,深度优先遍历
class Solution
{
vector
<int> res
;
public
:
vector
<int> inorderTraversal(TreeNode
* root
) {
if (root
){
inorderTraversal(root
->left
);
res
.push_back(root
->val
);
inorderTraversal(root
->right
);
}
return res
;
}
};
转载请注明原文地址: https://www.6miu.com/read-5034750.html