2022-5-1-每日一题-leetcode
作者:互联网
题目链接:https://leetcode-cn.com/problems/all-elements-in-two-binary-search-trees/
个人题解:中序遍历+排序
点击查看代码
/**
* 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;
void dfs(TreeNode* root)
{
if(!root) return;
dfs(root->left);
ans.push_back(root->val);
dfs(root->right);
}
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
dfs(root1);
dfs(root2);
sort(ans.begin(),ans.end());
return ans;
}
};
标签:right,TreeNode,val,dfs,2022,ans,一题,leetcode,left 来源: https://www.cnblogs.com/cytcnblogs/p/16212197.html