Same Tree (Java)
来源:互联网 发布:服装设计图纸软件 编辑:程序博客网 时间:2024/06/08 06:21
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.
Source
/** * Definition for binary tree * public 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 true; if(p == null && q != null) return false; if(p != null && q == null) return false; if(p.val != q.val) return false; return (isSameTree(p.left, q.left) && isSameTree(p.right, q.right)); }}
Test
public static void main(String[] args){ TreeNode a = new TreeNode(1); a.left = new TreeNode(2); a.right = new TreeNode(2); TreeNode b = new TreeNode(1); b.left = new TreeNode(2); b.right = new TreeNode(3); System.out.println(new Solution().isSameTree(a,b)); }
0 0
- [Leetcode] Same Tree (Java)
- LeetCode-Same Tree(java)
- Same Tree (Java)
- Leetcode Same Tree Java
- [LeetCode][Java] Same Tree
- LeetCode|Same Tree-java
- (java)leetcode Same Tree
- leetcode:Same Tree 【Java】
- Same Tree (JAVA实现)
- leetcode100 Same Tree java
- 100. Same Tree(Java)
- same-tree Java code
- LeetCode 100 -Same Tree ( JAVA )
- leetcode-java-100. Same Tree
- [LeetCode-100]Same Tree(java)
- 【leetcode】100. Same Tree【java】
- LeetCode-100. Same Tree (Java)
- 【leetcode】Same Tree and Symmetric Tree-----Java
- 15-OC中NSMutableDictionary的常见方法
- MATLAB多元非线性回归
- 概念重定义
- 剑指offer 32 整数中1出现的次数
- 16-OC中NSNumber的使用(把基本数据类型包装成OC对象)
- Same Tree (Java)
- HDU 5019 Revenge of GCD 第n个公共约数
- 17-OC中NSValue的使用(把NSValue类型成任意类型包装)
- 740 字符串处理
- Unity3d 真实地形仿真教程
- Android系统SystemServer进程启动过程源码分析
- bochs调试10-2.asm记录
- XStream实现javabean与xml之间相互转换
- JavaScript 程序设计基础(01) -- 创建对象的方式