Given two binary trees, write a function to check if they are equal or not.
2014-05-23 17:07
681 查看
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 leftsame=true;
boolean rifhtsame=true;
if(p==null&&q==null)
return true;
else if((p==null&&q!=null)||q==null&&p!=null)
return false;
else if(p.val!=q.val)
return false;
leftsame=isSameTree(p.left,q.left);
rifhtsame=isSameTree(p.right,q.right);
return leftsame&&rifhtsame;
}
}
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 leftsame=true;
boolean rifhtsame=true;
if(p==null&&q==null)
return true;
else if((p==null&&q!=null)||q==null&&p!=null)
return false;
else if(p.val!=q.val)
return false;
leftsame=isSameTree(p.left,q.left);
rifhtsame=isSameTree(p.right,q.right);
return leftsame&&rifhtsame;
}
}
相关文章推荐
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- [CrackCode] 1.4 Write a method to decide if two strings are anagrams or not
- Given an integer, write a function to determine if it is a power of two
- Check if the given binary tree is BST or not.
- A program to check if a binary tree is BST or not
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Check whether a given Binary Tree is Complete or not 解答
- Create a program the checks all the links in your web page whether they are active or not.
- If you are given two traversal sequences, can you construct the binary tree?
- anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg
- Adobe--Without using /,% ,+and * operators. write a function to check whether a number is divisible by 3 or nor?
- 1.3 Given two strings, write a method to decide if one is a permutation of the other.
- Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search
- org.apache.hadoop.hbase.DoNotRetryIOException: Class org.apache.phoenix.coprocessor.MetaDataEndpointImpl cannot be loaded Set hbase.table.sanity.checks to false at conf or table descriptor if you want
- check whether two binary trees are identical
- [面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
- 第15题 To Check the given linked list is palindrome or not
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- given two directory trees how to find which files are the same?