LeetCode 题解(223) : Symmetric Tree
来源:互联网 发布:c语言99乘法表位置 编辑:程序博客网 时间:2024/06/05 19:36
题目:
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 recursively and iteratively.
循环写的比较难看。
C++版:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSymmetric(TreeNode* root) { if(root == NULL || (root->left == NULL && root->right == NULL)) return true; return isSymmetric(root->left, root->right); } bool isSymmetric(TreeNode* first, TreeNode* second) { if((first == NULL && second != NULL) || (first != NULL && second == NULL)) return false; if(first == NULL && second == NULL) return true; if(first->val == second->val) { return isSymmetric(first->left, second->right) && isSymmetric(first->right, second->left); } return false; }};
Java版:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isSymmetric(TreeNode root) { if(root == null || (root.left == null && root.right == null)) return true; return isSymmetric(root.left, root.right); } public boolean isSymmetric(TreeNode first, TreeNode second) { if(first == null && second != null) return false; if(first != null && second == null) return false; if(first == null && second == null) return true; if(first.val == second.val) { return isSymmetric(first.left, second.right) && isSymmetric(first.right, second.left); } return false; }}
Python版:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneimport copyclass Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if root == None or (root.left == None and root.right == None): return True if root.left != None and root.right != None and root.left.val == root.right.val: ql1, ql2, qr1, qr2 = [root.left], [], [root.right], [] while len(ql1) != 0 or len(qr1) != 0: for i in range(len(ql1)): if ql1[i].left != None and qr1[-1 - i].right != None: if ql1[i].left.val != qr1[-1 - i].right.val: return False if (ql1[i].left != None and qr1[-1 - i].right == None) or (ql1[i].left == None and qr1[-1 - i].right != None): return False if ql1[i].right != None and qr1[-1 - i].left != None: if ql1[i].right.val != qr1[-1 - i].left.val: return False if (ql1[i].right != None and qr1[-1 - i].left == None) or (ql1[i].right == None and qr1[-1 - i].left != None): return False for i in ql1: if i.left != None: ql2.append(i.left) if i.right != None: ql2.append(i.right) for i in qr1: if i.left != None: qr2.append(i.left) if i.right != None: qr2.append(i.right) ql1 = copy.copy(ql2) qr1 = copy.copy(qr2) ql2 = [] qr2 = [] return True else: return False
0 0
- LeetCode 题解(223) : Symmetric Tree
- LeetCode题解:Symmetric Tree
- Leetcode题解 101. Symmetric Tree
- LeetCode题解-101-Symmetric Tree
- LeetCode 101. Symmetric Tree 题解
- LeetCode题解——Symmetric Tree
- LeetCode101. Symmetric Tree题解
- LeetCode题解:Symmetric Tree(有4种解法)
- Leetcode - Tree - Symmetric Tree
- Leetcode - Tree - Symmetric Tree
- LeetCode[Tree]: Symmetric Tree
- [leetcode][tree] Symmetric Tree
- LeetCode: Symmetric Tree
- [Leetcode] Symmetric Tree
- LeetCode : Symmetric Tree
- leetcode 13:symmetric tree
- [LeetCode] Symmetric Tree - Recursively
- [LeetCode] Symmetric Tree - Iteratively
- PHP的一个加密算法
- 如何分析解决Android ANR
- 内核与内核模块:depmod,lsmod,modinfo,insmod,rmmod,mdprobe
- 大众点评校招内推面试经历-测试开发岗位
- 人脸检测
- LeetCode 题解(223) : Symmetric Tree
- MyBatis简介
- Loadrunner接口测试基本操作方法
- 对于输入框为空及输入的东西只有空格的判断 ios
- PHP+Mysql+Sphinx高效的站内搜索引擎搭建详释
- VRRP技术白皮书
- NSMutableArray要怎样才能实现遍历删除?
- MySQL索引背后的数据结构及算法原理
- Unity3D研究院之Unity3D回馈IOS高级界面消息(十)