[leetcode]100. Same Tree@Java解题报告
2017-08-05 11:09
316 查看
https://leetcode.com/problems/same-tree/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.
package go.jacob.day805;
public class Demo3 {
/*
* 简单的递归实现
*/
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;
}
private class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
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.
package go.jacob.day805;
public class Demo3 {
/*
* 简单的递归实现
*/
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;
}
private class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
相关文章推荐
- LeetCode解题报告 100. Same Tree [easy]
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- [Leetcode] 100. Same Tree 解题报告
- [leetcode] 100. Same Tree 解题报告
- 【LeetCode】100. Same Tree 解题报告
- [leetcode]110. Balanced Binary Tree@Java解题报告
- 【LeetCode】100. Same Tree 解题报告
- [leetcode]98. Validate Binary Search Tree@Java解题报告
- 【LeetCode】Validate Binary Search Tree 解题报告(Java & Python)
- Maximum Depth of Binary Tree | leetcode 104 【Java解题报告】
- 【LeetCode】Binary Tree Paths 解题报告(java & python)
- Leetcode 100. Same Tree 验证树是否相同 解题报告
- LeetCode 100. Same Tree 解题报告
- 【LeetCode】98. Validate Binary Search Tree(Medium)解题报告
- leetcode解题之100# Same Tree Java版
- [leetcode]209. Minimum Size Subarray Sum@Java解题报告
- [leetcode]99. Recover Binary Search Tree@Java解题报告
- [leetcode] 101. Symmetric Tree 解题报告
- [leetcode] 270. Closest Binary Search Tree Value 解题报告
- 【LeetCode】Hamming Distance 解题报告(java & python)