101. 对称二叉树
最近更新:2025-02-02 | 字数总计:126 | 阅读估时:1分钟 | 阅读量:次
- 解析
- dfs同步判断法
Problem: 101. 对称二叉树
解析
dfs同步判断法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: bool isSymmetric(TreeNode *root) { TreeNode *node = root; return dfsJudge(node->left, node->right); } bool dfsJudge(TreeNode *nodeLeft, TreeNode *nodeRight) { if (nodeLeft == nullptr && nodeRight == nullptr) { return true; } else if (nodeLeft == nullptr || nodeRight == nullptr) { return false; } bool a = dfsJudge(nodeLeft->left, nodeRight->right); bool b = dfsJudge(nodeLeft->right, nodeRight->left); return a && b && (nodeLeft->val == nodeRight->val); } };
|