leetcode 101. Symmetric Tree
2017-07-04 09:48
417 查看
原题:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree
But the following
Note:
Bonus points if you could solve it both recursively and iteratively.
题目的意思就是写个算法判断下是不是对称
我的思路是移动,然后递归
代码如下:
bool isSymmetric(struct TreeNode* root) {
if(root==NULL)
return true;
if(root->left==NULL&&root->right==NULL)
return true;
if(root->left==NULL||root->right==NULL)
return false;
if(root->left->val==root->right->val)
{
struct TreeNode *p;
p=root->left->left;
root->left->left=root->right->left;
root->right->left=p;
if((isSymmetric(root->left)==false)||isSymmetric(root->right)==false)
return false;
else
return true;
}
return false;
}效率值:
还可以吧,看了下别人写的,大部分也都是递归,只不过递归的方式不太一样。
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree
[1,2,2,3,4,4,3]is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following
[1,2,2,null,3,null,3]is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题目的意思就是写个算法判断下是不是对称
我的思路是移动,然后递归
代码如下:
bool isSymmetric(struct TreeNode* root) {
if(root==NULL)
return true;
if(root->left==NULL&&root->right==NULL)
return true;
if(root->left==NULL||root->right==NULL)
return false;
if(root->left->val==root->right->val)
{
struct TreeNode *p;
p=root->left->left;
root->left->left=root->right->left;
root->right->left=p;
if((isSymmetric(root->left)==false)||isSymmetric(root->right)==false)
return false;
else
return true;
}
return false;
}效率值:
还可以吧,看了下别人写的,大部分也都是递归,只不过递归的方式不太一样。
相关文章推荐
- 2017.11.3 LeetCode - 101. Symmetric Tree
- leetcode 101. Symmetric(对称的) Tree
- LeetCode 101. Symmetric Tree
- leetcode 101. Symmetric Tree
- leetcode -- 101. Symmetric Tree 【对称树,结构,内容】
- leetcode 101. Symmetric Tree 判断对称树,递归和迭代
- leetcode 101. Symmetric Tree
- [leetCode By Python]101. Symmetric Tree
- LeetCode解题报告 101. Symmetric Tree [easy]
- Leetcode 101. Symmetric Tree
- [leetcode] 101. Symmetric Tree 解题报告
- LeetCode 101. Symmetric Tree (对称树)
- leetcode 101. Symmetric Tree-对称二叉数|深度遍历
- 小白笔记---------------------------------leetcode(101. Symmetric Tree )
- [LeetCode] 101. Symmetric Tree 对称树
- 【LeetCode】101. Symmetric Tree
- Leetcode 101. Symmetric Tree
- leetCode 101. Symmetric Tree 对称树
- [LeetCode]101. Symmetric Tree
- leetcode No101. Symmetric Tree