您的位置:首页 > 编程语言 > Java开发

leetcode100 Same Tree java

2017-03-21 16:44 369 查看
欢迎转载,转载请注明原地址:

http://blog.csdn.net/u013190088/article/details/64444050

Description

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.

解法

用递归,考虑好结束条件即可。

public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null) return true;
if(p ==null || q == null) return false;
if(p.val == q.val)
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
return false;
}


如果不用递归,用两个栈也可以解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode java