LeetCode 100. 相同的树(Same Tree) 2
100. 相同的树
100. Same Tree
题目描述
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
每日一算法2019/5/5Day 2LeetCode100. Same Tree
Java 实现
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null || q == null) { return p == null && q == null; } if (p.val == q.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } return false; } }
参考资料
- https://leetcode.com/problems/same-tree/
- https://leetcode-cn.com/problems/same-tree/
转载于//www.cnblogs.com/hglibin/p/10817039.html
还没有评论,来说两句吧...