leetcode[101]:Symmetric Tree
2015-06-18 16:08
369 查看
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Note:
Bonus points if you could solve it both recursively and iteratively.
根节点单独判断,后面的只需要判断左子树的左子树和右子树的右子树以及左子树的右子树和右子树的左子树即可。
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Note:
Bonus points if you could solve it both recursively and iteratively.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool compare(struct TreeNode* Tleft,struct TreeNode* Tright){ if(Tleft->val != Tright->val ) return false; if(Tleft->left && !Tright->right || !Tleft->left && Tright->right || Tleft->right && !Tright->left ||!Tleft->right && Tright->left) return false; if(Tleft->left && Tright->right ) if(!compare(Tleft->left,Tright->right)) return false; if(Tleft->right && Tright->left ) if(!compare(Tleft->right,Tright->left)) return false; return true; } bool isSymmetric(struct TreeNode* root) { if(!root) return true; if(!root->left && !root->right) return true; if(root->left && root->right) return compare(root->left,root->right); else return false; }
根节点单独判断,后面的只需要判断左子树的左子树和右子树的右子树以及左子树的右子树和右子树的左子树即可。
相关文章推荐
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- Binary Tree Zigzag Level Order Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- Flatten Binary Tree to Linked List
- Populating Next Right Pointers in Each Node II
- Gray Code
- Add Binary @ LeetCode java
- Mysql查询大小写不敏感简便解决办法
- Add Binary
- cf.256.div2.D
- Remove Node in Binary Search Tree
- linux 逆向
- Leetcode No.50: Pow(x, n)
- Binary Tree Postorder Traversal