Given a binary tree, check whether it is a mirror of itself.
For example, the binary tree[1,2,2,3,4,4,3] is a symmetric tree.
Notes
bonus points if you could solve it both recursively and iteratively.
/*
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
*/
class solution{
public:
bool isSymmetric(TreeNode *root)(
if(!root)
return true;
else
return compareChild(root->left, root->right)
private:
bool compareChild(TreeNode *rroot, TreeNode *lroot){
if(!lroot)
return rroot == NULL;
if(rroot == NULL)
return false;
if(lroot->val != rroot->val)
return false;
return compareChild(rroot->left, lroot->right) && compareChild(rroot->right, lroot->left);
}
}
本文介绍了一种检查二叉树是否为对称的方法。通过递归比较左右子树节点来实现,当根节点为空时返回真;若左右子树不同时为空且值相等,则继续比较它们的子节点。
369

被折叠的 条评论
为什么被折叠?



