leetcode 101 对称二叉树
题目
递归解法
关键:对称二叉树的先序遍历和先右后左的先序遍历结果完全一样
C++
/** * 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(root == NULL) return true; return dfs(root->left, root->right); } bool dfs(TreeNode *lt, TreeNode *rt) { if(lt == NULL && rt == NULL) return true; if(lt && rt && lt->val == rt->val) return dfs(lt->left, rt->right) && dfs(lt->right, rt->left); return false; } };
非递归解法
二叉树非递归遍历,待实现