?LeetCode刷題實(shí)戰(zhàn)101:對(duì)稱(chēng)二叉樹(shù)
算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !
今天和大家聊的問(wèn)題叫做?對(duì)稱(chēng)二叉樹(shù),我們先來(lái)看題面:
https://leetcode-cn.com/problems/symmetric-tree/
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
題意

解題


class?Solution?{
????public?boolean?isSymmetric(TreeNode root)?{
????????return?check(root, root);
????}
????public?boolean?check(TreeNode p, TreeNode q)?{
????????if?(p == null?&& q == null) {
????????????return?true;
????????}
????????if?(p == null?|| q == null) {
????????????return?false;
????????}
????????return?p.val == q.val && check(p.left, q.right) && check(p.right, q.left);
????}
}
上期推文:
評(píng)論
圖片
表情
