leetcode_100번--Same Tree(두 갈래 나무의 귀속-선착순)

1778 단어 LeetCode

Same Tree

 
Total Accepted: 56263 Total Submissions: 133912 My Submissions
Question
 Solution 
 
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
 
Hide Tags
 
Tree   Depth-first Search
Have you met this question in a real interview? 
Yes
 
No
 
Discuss
#include<iostream>

#include<vector>

using namespace std;



struct TreeNode {

      int val;

      TreeNode *left;

      TreeNode *right;

      TreeNode(int x) : val(x), left(NULL), right(NULL) {}

};



// AC, , , 

//vector , vector, '#', 

// asii , 

void first_search(TreeNode* root,vector<int>& vec)

{

	if(root!=NULL)

		vec.push_back(root->val);

	else

	{vec.push_back('#');return;}

	first_search(root->left,vec);

	first_search(root->right,vec);

	return;

}

bool isSameTree(TreeNode *p, TreeNode *q)

{

	vector<int> vec1,vec2;

	first_search(p,vec1);

	first_search(q,vec2);

	if(vec1==vec2)

		return 1;

	else

		return 0;

}





int main()

{



}


좋은 웹페이지 즐겨찾기