難度:簡單/Easy
序號與題目:100——相同的樹
給定兩個二叉樹,編寫一個函數來檢驗它們是否相同。
如果兩個樹在結構上相同,並且節點具有相同的值,則認爲它們是相同的。
示例 1:
輸入: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
輸出: true
示例 2:
輸入: 1 1
/ \
2 2
[1,2], [1,null,2]
輸出: false
示例 3:
輸入: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
輸出: false
思考:針對兩棵樹的根節點,有下列四種情況:p 和 q 都爲空,兩棵樹相同;p 不爲空 q 爲空,兩棵樹不相同;p 爲空 q 不爲空,兩棵樹不相同;p 和 q 都不爲空,如果兩個節點的值相同,而且遞歸判斷左右子樹也相同的話,兩棵樹相同,反之兩棵樹不同。
實現:
C
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
if(p==NULL && q==NULL)
{
return true;
}
if(p!=NULL && q!=NULL && p->val==q->val )
{
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
else
{
return false;
}
}
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
bool isSameTree(TreeNode* p, TreeNode* q)
{
if(p==NULL && q==NULL)
return true;
if(p!=NULL && q!=NULL && p->val==q->val )
{
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
else
{
return false;
}
}
};
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public boolean isSameTree(TreeNode p, TreeNode q)
{
if(p==null && q==null)
{
return true;
}
if(p!=null && q!=null && p.val==q.val )
{
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
else
{
return false;
}
}
}
Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p and not q:
return True
elif p is not None and q is not None:
if p.val == q.val:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
else:
return False
else:
return False
序號與題目:101——對稱二叉樹
給定一個二叉樹,檢查它是否是鏡像對稱的。
例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面這個 [1,2,2,null,3,null,3] 則不是鏡像對稱的:
1
/ \
2 2
\ \
3 3
說明:
如果你可以運用遞歸和迭代兩種方法解決這個問題,會很加分。
思考:如果兩棵樹均爲空,返回爲真。一棵樹爲空,一棵樹不爲空,返回爲假。兩個樹都不爲空,根的值不同,返回爲假,根的值相同,對於同一位置的根,判斷一棵樹根的左子樹的值是否與另一棵樹的右子樹的值,或者是判斷一棵樹根的右子樹的值是否與另一棵樹的左子樹的值,不同返回假,相同則判斷下一個根(遞歸)
實現:
C
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool dfs(struct TreeNode* t1, struct TreeNode* t2)
{
if (!t1 && !t2)
return true;
if (!t1 || !t2)
return false;
if (t1->val != t2->val)
return false;
return dfs(t1->left, t2->right) && dfs(t1->right, t2->left);
}
bool isSymmetric(struct TreeNode* root)
{
if (!root)
return true;
return dfs(root->left, root->right);
}
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
bool isSymmetric(TreeNode* root)
{
return ismirror(root,root);
}
bool ismirror(TreeNode* p,TreeNode* q)
{
if(!p&&!q)//都爲NULL
return true;
if(!p||!q)//有一個爲NULL
return false;
if(p->val==q->val)
return ismirror(p->left,q->right)&&ismirror(p->right,q->left);
return false;
}
};
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public boolean isSymmetric(TreeNode root)
{
return isMirror(root, root);
}
public boolean isMirror(TreeNode t1, TreeNode t2)
{
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return (t1.val == t2.val)
&& isMirror(t1.right, t2.left)
&& isMirror(t1.left, t2.right);
}
}
Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root:
return self.digui(root.left,root.right)
else:
return True
def digui(self,q,p):
if q and p:
return q.val==p.val and self.digui(q.left,p.right) and self.digui(q.right,p.left)
else:
return not q and not p