力扣—二叉树的前序遍历
题目要求:
给你二叉树的根节点 root ,返回它节点值的前序遍历。
(题目来源:力扣)


int TreeSize(struct TreeNode* root)
{
return root == NULL? 0 : TreeSize(root->left)+TreeSize(root->right)+1;
}
void preorder(struct TreeNode* root,int* arr,int* i)
{
if(root == NULL)
return 0;
a[(*i)++] = root->val;
preorder(root->left,arr,i);
preorder(root->right,arr,i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = TreeSize(root);
int* arr = (int *)malloc(sizeof(int)* (*returnSize));
int i = 0;
preorder(root,arr,&i);
return arr;
}
最后
以上就是坚定灰狼最近收集整理的关于力扣---二叉树的前序遍历的全部内容,更多相关力扣---二叉树内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复