English title
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
Chinese title
Given a binary tree, check if it is mirror-symmetrical.
For example, a binary tree [1,2,2,3,4,4,3]
is symmetrical.
1
/ \
2 2
/ \ / \
3 4 4 3
However, the following [1,2,2,null,3,null,3]
is not a mirror image:
1
/ \
2 2
\ \
3 3
Algorithm
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
//镜像对称的递归函数
public bool 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.left, t2.right)
&& IsMirror(t1.right, t2.left);
}
Experimental results
Related graphic :
- LeetCode combat: deleting the penultimate N nodes linked list
- LeetCode combat: merge two ordered lists
- LeetCode combat: switching node in the linked list twenty-two
- LeetCode combat: rotating list
- How to use C # to achieve K nearest neighbor algorithm?
- How to use C # achieve the KD Tree structure?
- How to use C # + KDTree achieve K nearest neighbor algorithm?
- How to use neural network model abstract C #?
- How to use C # to achieve perceptual model neural network?
- How to use C # to achieve Delta learning rule?
- How to use C # crawling One holder rebate data!
- How to use C # crawling BigOne Exchange Bulletin!
- How to use C # crawling ONE of transaction data?
- How to use C # crawling "Jingdong - Computer and Internet book sales chart!"
- How to use C # crawling "Dangdang - Computer and Internet book sales chart!"
- How to use C # crawling "Cat's Eye Pro film: box office," Data!
- How to use C # crawling "Cat's Eye Film: domestic box office" and the corresponding video information!
- How to use C # crawling with Token verified site data?
Reproduced in: https: //www.jianshu.com/p/4e8e0301d402