leetcode 101. Symmetric Tree
2015-02-17 17:26
357 查看
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
Note:
Bonus points if you could solve it both recursively and iteratively.
[Solution]
递归判断
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.
[Solution]
递归判断
symCheck(left->left, right->right) && symCheck(left->right, right->left)
bool isSymmetric(TreeNode *root) { if (root == NULL) return true; return symCheck(root->left, root->right); } bool symCheck(TreeNode *left, TreeNode *right) { if (left == NULL || right == NULL) { if (left == right) return true; else return fasle; } if (left->val == right->val) return symCheck(left->left, right->right) && symCheck(left->right, right->left); else return false; }
相关文章推荐
- [勇者闯LeetCode] 101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- LeetCode *** 101. Symmetric Tree
- Leetcode 101. Symmetric Tree
- LeetCode解题报告 101. Symmetric Tree [easy]
- Leetcode 101. Symmetric Tree
- leetcode 101. Symmetric Tree(C语言,二叉树,递归,判对称性)30
- leetcode 101. Symmetric Tree
- 【LeetCode】101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- leetcode 101. Symmetric Tree
- LeetCode101. Symmetric Tree
- leetcode 每日一题 101. Symmetric Tree
- Leetcode 101. Symmetric Tree
- leetcode 101. Symmetric Tree
- LeetCode101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- LeetCode 101. Symmetric Tree
- leetcode101. Symmetric Tree
- 小白笔记---------------------------------leetcode(101. Symmetric Tree )