2叉树
来源:互联网 发布:python enumerate 编辑:程序博客网 时间:2024/06/07 22:32
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
分析:
题目是要判断左右两颗子树是否对称,采用只要根节点的值相同,并且左边子树的左子树和右边子树饿右子树对称 且 左边子树的右子树和右边子树的左子树对称。采用树的遍历的思想,递归的解决该问题。
转载于:http://blog.csdn.net/pwiling/article/details/50868313
这里写代码片class Solution {public: bool isSymmetric(TreeNode* root) { if(root==NULL||(root->left==NULL&&root->right==NULL)) return true; return postorder(root->left,root->right); } bool postorder(TreeNode* p,TreeNode* q) { if(p&&q) { if(p->val!=q->val) return false; return postorder(p->left,q->right)&&postorder(p->right,q->left); }else if(p==NULL&&q==NULL) return true; else return false; }};
2 0
- 2叉树
- 层次遍历2叉树
- 层次遍历2叉树
- 完全2叉树 应用
- 完全2叉树 小球下落 非模拟方法
- 16叉树
- 表达式2叉树(前序,中序,后序)的非递归实现_模板实现
- 表达式2叉树(前序,中序,后序)的非递归实现_优化1
- [算法] 红黑树比一般的平衡2叉树,到底有什么特殊的优势和作用?
- idr机制(32叉树)
- idr机制(32叉树)
- K 叉树( ktree )
- idr机制(32叉树)
- 【51Nod1582】 n叉树
- (转)N叉树 C#
- 树权-最小生成树-叉树
- 51Nod-1582-n叉树
- 着色问题,满m叉树
- 【数据库】MySQL数据库简介及安装
- JavaWeb基础——细节知识汇总
- 一些重要的算法The Most Important Algorithms
- 计算机图形学作业——DDA算法实现
- [LeetCode]110. Balanced Binary Tree
- 2叉树
- 产品经理的素质(集思广益探讨版)
- didMoveToSuperview
- PHP 数组浅析
- POJ2676 Sudoku
- 索引之tf-idf
- 交换机和路由器的区别
- Best practices for the logging REST API
- ios-提升之【7】-定时器