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}" 1 recursively 递归法: //2014-2-15 update bool isSymmetric(TreeNode *root) { if (!root) return true; return isSymmetric(root->left, root->right); } bool isSymmetric(TreeNode *lt, TreeNode *rt) { if (!lt && !rt) return true; if (lt && !rt || !lt && rt || lt->val !
Read full article from Leetcode Symmetric Tree 递归和非递归解法 - 靖空间 - 博客频道 - CSDN.NET
No comments:
Post a Comment