中序、前序、后序是指对根节点的访问顺序,孩子结点无论哪种遍历方式,都是先左后右!!!
中序遍历
左 中 右
class Solution {
public:
int inorder(TreeNode* root)
{
if (root != NULL)
{
inorder(root->left, vec_res);
printf("%d\n", root->val);
inorder(root->right, vec_res);
}
return 0;
}
}
前序遍历
中 左 右
class Solution {
public:
int inorder(TreeNode* root)
{
if (root != NULL)
{
printf("%d\n", root->val);
inorder(root->left, vec_res);
inorder(root->right, vec_res);
}
return 0;
}
}
后序遍历
左 右 中
class Solution {
public:
int inorder(TreeNode* root)
{
if (root != NULL)
{
inorder(root->left, vec_res);
inorder(root->right, vec_res);
printf("%d\n", root->val);
}
return 0;
}
}