题目描述
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
题目大意:判断两棵二叉树是否相同
样例
链接,自己去看
python解法
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
import queue
tree1 = queue.Queue()
tree2 = queue.Queue()
if not p and not q:
return True
if not p or not q:
return False
tree1.put(p)
tree2.put(q)
while(not tree1.empty()):
x = tree1.get()
y = tree2.get()
if x.val != y.val:
return False
elif x.left and not y.left:
return False
elif not x.left and y.left:
return False
elif x.left and y.left:
tree1.put(x.left)
tree2.put(y.left)
if x.right and not y.right:
return False
elif not x.right and y.right:
return False
elif x.right and y.right:
tree1.put(x.right)
tree2.put(y.right)
return True
Runtime: 44 ms, faster than 10.15% of Python3 online submissions for Same Tree.
Memory Usage: 14.1 MB, less than 5.72% of Python3 online submissions for Same Tree.
题后反思:
- python的代码中尽量不要有全局变量
C语言解法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct Queue {
struct TreeNode * Node;
struct Queue * next;
};
void push(struct Queue *head, struct TreeNode*p)
{
struct Queue *h = head;
while(h -> next)
{
h = h -> next;
}
h -> next = (struct Queue *)malloc(sizeof(struct Queue ));
h -> next -> next = NULL;
h -> next -> Node = p;
}
struct TreeNode* pop(struct Queue *head)
{
return head -> Node;
}
bool empty(struct Queue * head)
{
return head ? false : true;
}
void clear(struct Queue * head)
{
struct Queue *p = NULL;
while(head)
{
p = head;
head = head -> next;
free(p);
}
}
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if (!p && !q)
{
return true;
}
if (!p || !q)
{
return false;
}
struct TreeNode*r = p, *t = p;
struct Queue *temp = NULL;
struct Queue *tree1 = (struct Queue *)malloc(sizeof(struct Queue));
tree1 -> Node = p;
tree1 -> next = NULL;
struct Queue *tree2 = (struct Queue *)malloc(sizeof(struct Queue));
tree2 -> Node = q;
tree2 -> next = NULL;
while(!empty(tree1))
{
r = pop(tree1);
q = pop(tree2);
if (r -> val != q -> val)
{
clear(tree1);
clear(tree2);
return false;
}
else if (!r -> left && q -> left)
{
clear(tree1);
clear(tree2);
return false;
}
else if (r -> left && !q -> left)
{
clear(tree1);
clear(tree2);
return false;
}
else if(r -> left && q -> left)
{
push(tree1, r -> left);
push(tree2, q -> left);
}
if (!r -> right && q -> right)
{
clear(tree1);
clear(tree2);
return false;
}
else if (r -> right && !q -> right)
{
clear(tree1);
clear(tree2);
return false;
}
else if(r -> right && q -> right)
{
push(tree1, r -> right);
push(tree2, q -> right);
}
temp = tree1;
tree1 = tree1 -> next;
free(temp);
temp = tree2;
tree2 = tree2 -> next;
free(temp);
}
return true;
}
Runtime: 0 ms, faster than 100.00% of C online submissions for Same Tree.
Memory Usage: 7.2 MB, less than 8.33% of C online submissions for Same Tree.
题后反思:
- 这道题涉及二叉树的层次遍历,所以使用链队列解决问题
- 在return false的 if 判断中,可以将条件放在一块,但是我觉得这样写 if 中的内容太多,所以分开了
- 队列pop元素之后,要记得清空内存,但是最好在循环的最后清空,否则会出现空指针异常
- 在return false之前,最好也有清空内存的语句。
文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步