- 首先我要在纸上,非常非常聪明且迅速且机灵,
- 给出几个用例,找出边界用例和特殊用例,确定特判条件;在编码前考虑到所有的条件
- 向面试官提问:问题规模,特殊用例
- 给出函数头
- 暴力解,简述,优化。
- 给出能够想到的最优价
- 伪代码,同时结合用例
- 真实代码
Same Tree
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.
// Same Tree
// 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.
struct Node
{
Node * left;
Node * right;
T value;
}
bool same_tree (Node* a, Node* b)
{
if( a==NULL && b==NULL) return true;
else if( a==NULL || b==NULL) return false;
if( a->value != b->value ) return false;
return same_tree( a->left, b->left) && same_tree( a->right, b->right);
}