题解 | #牛群的相似结构#
牛群的相似结构
https://www.nowcoder.com/practice/ecaeef0d218440d295d9eff63fbc747c
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ #include <unistd.h> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param p TreeNode类 * @param q TreeNode类 * @return bool布尔型 */ void travel(TreeNode* root,vector<int>& ans) { if (root == NULL) { ans.push_back(0); return; } ans.push_back(root->val); travel(root->left, ans); travel(root->right, ans); } bool isSameTree(TreeNode* p, TreeNode* q) { // write code here vector<int>data1; vector<int>data2; travel(p, data1); travel(q, data2); if (data1.size() != data2.size()) { return false; } for (int i = 0;i < data1.size();++i) { if (data1[i] != data2[i]) { return false; } } return true; } };
傻人写傻代码