LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode *root) {
13         if(!root)
14             return true;    //无根结点
15         else if(root->left==(void*)0 && root->right==(void*)0)        //只有根结点的树
16             return true;
17         else if(root->left!=(void*)0 && root->right!=(void*)0 && isSymmetric(root->left,root->right))    //两个子树非空,左右子树为对称
18             return true;
19         else
20             return false;
21     }
22     bool isSymmetric(TreeNode *left,TreeNode *right) {
23         if(left->val==right->val){        //左右结点值相等
24             if(left->left==(void*)0 && right->right==(void*)0 || left->left!=(void*)0 && right->right!=(void*)0 && isSymmetric(left->left,right->right)){        //左右子树都为空、或者对称
25                 if(left->right==(void*)0 && right->left==(void*)0 ||left->right!=(void*)0 && right->left!=(void*)0 && isSymmetric(left->right,right->left))      //右左子树都为空、或对称
26                     return true;
27                 else
28                     return false;
29             }
30             else    //左右子树不对称
31                 return false;
32         }
33         else //左右结点值不等
34             return false;
35     }
36 };

本题有多种解法,递归的,非递归的。

而上面代码是递归法。

思路:

主要判断左子树与右子树。

在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较。

到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了。

当某个结点对称了,它的左子树也对称了,右子树也对称了,那才是真的对称了。

时间: 2024-08-05 19:32:38

LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)的相关文章

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

LeetCode OJ - Symmetric Tree && Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

[LeetCode OJ] Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

101. Symmetric Tree -- 判断树结构是否对称

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

Leetcode:Symmetric Tree 判断对称树

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

[LeetCode 题解]: Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could solve it both recu

LeetCode OJ - Same Tree

题目: 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. 解题思路: 递归求解.先判断根,然后递归判断左子树和右子树 代码 1 /** 2 * Definition for bina

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *