[leetcode][tree] Symmetric Tree
2015-05-20 20:04
169 查看
题目:
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.
递归实现
非递归实现(方法类似于中序遍历)
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.
递归实现
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSymmetric(TreeNode* root) { if(NULL == root) return true; return isSymetricCore(root->left, root->right); } private: bool isSymetricCore(TreeNode *left, TreeNode *right){ if(NULL == left && NULL == right) return true; if(NULL == left || NULL == right || left->val != right->val) return false; return isSymetricCore(left->left, right->right) && isSymetricCore(left->right, right->left); } };
非递归实现(方法类似于中序遍历)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSymmetric(TreeNode* root) { if (NULL == root || NULL == root->left && NULL == root->right) return true; if (NULL == root->left || NULL == root->right) return false; stack<TreeNode *> leftStack, rightStack; leftStack.push(root->left); rightStack.push(root->right); TreeNode *pLeft = root->left; TreeNode *pRight = root->right; while (pLeft && pRight || !leftStack.empty() && !rightStack.empty()){ while (pLeft && pRight){ leftStack.push(pLeft); rightStack.push(pRight); pLeft = pLeft->left; pRight = pRight->right; } if (pLeft || pRight) return false; pLeft = leftStack.top(); pRight = rightStack.top(); leftStack.pop(); rightStack.pop(); if (pLeft->val != pRight->val) return false; pLeft = pLeft->right; pRight = pRight->left; } if (pLeft || pRight || !leftStack.empty() || !rightStack.empty()) return false; return true; } };
相关文章推荐
- 【LeetCode从零单排】No100 Same Tree && No101 Symmetric Tree
- Leetcode - Tree - Symmetric Tree
- LeetCode-Same Tree & Symmetric Tree
- leetcode_101题——Symmetric Tree (树tree,递归,还有迭代没想出来)
- LeetCode[Tree]: Symmetric Tree
- Leetcode - Tree - Symmetric Tree
- 【Leetcode】Symmetric Tree (Tree Judge)
- LeetCode Binary Tree Level Order Traversal
- [Leetcode] Recover Binary Search Tree
- LeetCode: isSameTree1 解题报告
- Leetcode:minimum-depth-of-binary-tree
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- leetcode-Binary Tree Zigzag Level Order Traversal
- leetcode - Binary Tree Postorder Traversal
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode#104 Maximum Depth of Binary Tree
- Leetcode-107(Java) Binary Tree Level Order Traversal II
- LeetCode Binary Tree Inorder Traversal 中序遍历二叉树
- Binary Tree Preorder Traversal leetcode144
- 和大神们学习每天一题(leetcode)-Minimum Depth of Binary Tree