symmetric-tree
来源:互联网 发布:judy兔子 知乎 编辑:程序博客网 时间:2024/06/07 13:29
题目描述
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
\
5
The above binary tree is serialized as”{1,2,3,#,#,4,#,#,5}”.
/** * Definition for binary tree * 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) return true; return recurjudge(root->left,root->right); } bool recurjudge(TreeNode* left,TreeNode* right) { if(left==NULL&&right==NULL) return true; if(left==NULL||right==NULL) return false; if(left->val!=right->val) return false; return recurjudge(left->left,right->right)&recurjudge(left->right,right->left); }};
0 0
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- Symmetric Tree
- 如何在library中使用productFlavors
- Driver/Firmware/NVM/PHYID of Intel adapters in purley server
- 【TensorFlow_Fold】计算图连接初探 Diamond计算图调试历程
- android学习笔记(6)
- VR视频直播播放器
- symmetric-tree
- 删除表+闪回删除+查询数据字典+查看回收站删除的表+清空回收站
- poj 2456 Aggressive cows (二分求最大值)
- 解决Ubuntu14.04自带拼音输入问题
- Python操作MySQL数据库的三种方法
- git status 显示中文乱码
- 欢迎使用CSDN-markdown编辑器
- 关于图片
- nginx 在加载thinkphp5的时候 出现404