对称树

闲着没事,做两道题玩玩,有一些地方还是有一些意思的;

 1 ```c++
 2 bool isSymmetric(TreeNode *root)
 3 {
 4 //约定空树是对称的
 5 if(root==NULL)
 6 return true;
 7 else
 8 return isEqual(root->left,root->right);
 9 }
10 //判断是否对称,应该是有两个指针去指的,因此写一个两个指针参数的判断函数,递归的进行判断
11 bool isEqual(TreeNode *a,TreeNode *b)
12 {
13 //当前两个结点均为空,则这一步的判断上是对称的
14 if(a==NULL && b==NULL)
15 return true;
16 //只有一个为空,另一个不为空,显然不对称
17 else if(a==NULL || b==NULL)
18 return false;
19 //如果当前的两个结点非空且相等,则递归的判断他们的左右-右左结点
20 if(a->val == b->val)
21 return isEqual(a->left,b->right)&&isEqual(a->right,b->left);
22 else return false;
23 }
24 ```

其实对称树就是从根节点开始,用两个指针分别指针两边,然后判断是否相等(包括值相等、或者均为空),就是isEqual函数,用递归写的,也很简洁。

时间: 2024-12-14 23:55:50

对称树的相关文章

对称树 Symmetric Tree

算法能力一直不好,决定利用晚上和假期时间多做点算法题,动动手,提高自己.大家看到我的水平低还望莫要嘲笑,嘻嘻. 这一题呢是LeetCode上的对称树问题, 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 fol

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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:

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 方法一: 层次遍历是最直观的方法.对数进行层次遍历,记录每一层的节点,然后对每一层的value组成

LeetCode 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 [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 Note:Bonus

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) {

leetCode 101. Symmetric Tree 对称树

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 [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]

Symmetric Tree,对称树

问题描述: 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 算法分析:

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