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组成的字符串判断是不是对称串。算法的时间复杂度为O(nlgn),非最优,侥幸AC。
1 class Solution { 2 public: 3 bool isSymmetric(TreeNode *root) { 4 if(!root) 5 return true; 6 if(root->left==NULL && root->right!=NULL || root->left!=NULL && root->right==NULL) 7 return false; 8 if(!root->left && !root->right) 9 return true; 10 mm.insert(make_pair(root->left,root->right)); 11 return judge(); 12 } 13 private: 14 multimap<TreeNode*,TreeNode*> mm; //存放每层的节点,将对称位置上的一对节点存在一个key-value对里面 15 16 bool judge(){ 17 if(mm.empty()) 18 return true; 19 multimap<TreeNode*,TreeNode*> tmp(mm); 20 mm.clear(); 21 for(multimap<TreeNode*,TreeNode*>::iterator it=tmp.begin();it!=tmp.end();++it){ 22 if(it->first->val!=it->second->val) 23 return false; 24 if(it->first->left && !it->second->right) 25 return false; 26 if(!it->first->left && it->second->right) 27 return false; 28 if(it->first->right && !it->second->left) 29 return false; 30 if(!it->first->right && it->second->left) 31 return false; 32 if(it->first->right && it->second->left) 33 mm.insert(make_pair(it->first->right,it->second->left)); 34 if(it->first->left && it->second->right) 35 mm.insert(make_pair(it->first->left,it->second->right)); 36 } 37 return judge(); //递归到树的下一层 38 } 39 };
方法二:
不采用层次遍历。直接比较对称位置:left的right和right的left比较,left的left和right的right比较。时间复杂度O(n)下面给出递归和非递归两个版本:
1、递归版本
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==NULL) return true; 14 return isSymmetric(root->left,root->right); 15 } 16 bool isSymmetric(TreeNode *left, TreeNode *right){ 17 if(left==NULL&&right==NULL) return true; 18 if(left==NULL||right==NULL) return false; 19 if(left->val!=right->val) return false; 20 return isSymmetric(left->left,right->right)&&isSymmetric(left->right,right->left); 21 } 22 };
2、非递归版本
1 class Solution { 2 public: 3 bool isSymmetric (TreeNode* root) { 4 if (!root) return true; 5 stack<TreeNode*> s; 6 s.push(root->left); 7 s.push(root->right); 8 while (!s.empty ()) { 9 auto p = s.top (); s.pop(); 10 auto q = s.top (); s.pop(); 11 if (!p && !q) continue; 12 if (!p || !q) return false; 13 if (p->val != q->val) return false; 14 s.push(p->left); 15 s.push(q->right); 16 s.push(p->right); 17 s.push(q->left); 18 } 19 return true; 20 } 21 };
转载于:https://www.cnblogs.com/zl1991/p/7045756.html
leetcode-Symmetric Tree 对称树相关推荐
- symmetric tree java_Symmetric Tree对称树
Easy 判断一棵二叉树是否对称 Example, 二叉树[1,2,2,3,4,4,3] 对称: 1 / 2 2 / \ / 3 4 4 3 二叉树[1,2,2,null,3,null,3]不对称 1 ...
- [leetcode]Symmetric Tree
问题叙述性说明: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its cente ...
- 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /** * Definition for a binary tree no ...
- LeetCode 101. Symmetric Tree
LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...
- 【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree
题目 1.same tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...
- LeetCode 572. 另一个树的子树 思考分析
题目 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: 给定 ...
- Symmetric Tree (101)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
最新文章
- hdu4539 郑厂长系列故事——排兵布阵 + POJ1158 炮兵阵地
- django ContentType使用方法
- Effective Java之用enum代替int常量(三十)
- leetcode-Excel Sheet Column Title
- How mBDOC is generated and saved in DB
- 自己动手系列——实现一个简单的ArrayList
- 认识下这位全能型漏洞猎人
- rman备份优化思路
- nagios监控3306端口
- 面包板电源线怎么接_电路实验中,直接用面包板连接线将电源短路会产生什么问题?...
- moments音标_moment的意思在线翻译,解释moment中文英文含义,短语词组,音标读音,例句,词源,同义词【澳典网ODict.Net】...
- COGS 1008 贪婪大陆
- 2023 XL软件库App后端源码 可自定义易支付 完整版
- python打印26个英文字母和数字
- icon和文本垂直居中对齐
- c语言单片机温度调节系统设计,基于单片机的温度控制器系统总体设计方案
- 电脑中了virus.win32.sality.i和virus.win32.pioneer.c病毒,导致所以的EXE文件打不开(打开浏览器会提示:xc0000005异常)
- VMware Workstation虚拟机配置
- AndroidStudo格式化快捷键(Ctrl+alt+L)失效·解决方法
- 标准字头密码体制c语言,现代密码学知识点整理:.doc