leetcode | Symmetric Tree
来源:互联网 发布:帝国时代2mac版 编辑:程序博客网 时间:2024/04/29 03:19
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.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
./** * 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) return true; else return isSame(root.left,root.right); } public boolean isSame(TreeNode l,TreeNode r) { if(l==null||r==null) return l==r; else return l.val==r.val&&isSame(l.left,r.right)&&isSame(l.right,r.left); }}
0 0
- 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
- [LeetCode] Symmetric Tree
- [LeetCode]Symmetric Tree
- Leetcode:Symmetric Tree
- [Leetcode]Symmetric Tree
- Leetcode: Symmetric Tree
- [leetcode]Symmetric Tree
- LeetCode-Symmetric Tree
- [leetcode] Symmetric Tree
- Session的用法(代码解析)
- 15_10_4
- IOS开发笔记 ——@dynamic 和 @synthesize 和区别
- linux c 获取硬盘序列号
- HTTPS协议简介
- leetcode | Symmetric Tree
- 杭电acm1302
- 零碎知识点(未分类)
- Python爬虫的一些总结
- 黑马程序员—OC语言的self关键字和super关键字
- XML数据解析-SAX和DOM
- LintCode 丢失的第一个正整数
- 如何盗取别人的QQ密码
- Android开发基础之-----------补间动画