LeetCode刷题之三:判断两个二叉树是否相同
2014-12-12 23:19
387 查看
题目为:
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.
解题思路:这种题目也是递归操作简单
代码为:
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.
解题思路:这种题目也是递归操作简单
代码为:
/** * 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) { boolean flag = false; 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); } }
相关文章推荐
- LeetCode基础--二叉树-判断两个树是否相同
- 每天一道LeetCode-----判断两个二叉树是否相同
- [leetcode 318]Maximum Product of Word Lengths--判断两个字符串是否有相同的字符
- 用Java代码写一个判断两个二叉树是否相同
- Java判断两个二叉树是否相同
- leetcode解题之242# Valid Anagram Java版 (判断两个字母串是否具有相同的字母组合)
- 算法 - 判断两个二叉树是否相同
- Leetcode Same Tree 判断两棵二叉树是否相同
- Leetcode 101. Symmetric Tree 判断二叉树是否对称,注意写在两个方法中,递归时注意空的判断
- [Swift]判断两个二叉树是否相同
- 二叉树面试题(一)---判断两个二叉树结构是否相同
- 判断两个二叉树是否结构相同
- 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】
- 【LeetCode91-100】编码种数,逆转单链表,IP地址转化,中序遍历二叉树,生成二叉搜索树,计算二叉树个数,交叉string【hard】,判断二叉搜索树是否合法,恢复二叉树(有两个元素被交换)
- [leetcode]Same Tree(判断两个二叉树是否相等 C语言实现)
- 判断两个二叉树是否相同
- [LeetCode]100. Same Tree--判断二叉树是否相同
- 判断两个二叉树是否相同
- java 判断两个文件是否相同
- VB.Net判断两个对象是否相同的方法