101. 对称二叉树
给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3] 输出:false
提示:
- 树中节点数目在范围
[1, 1000]
内 -100 <= Node.val <= 100
解析:
每次传入两个参数即可
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool dfs(TreeNode* &lchild, TreeNode* &rchild) { if(lchild == nullptr && rchild == nullptr) return true; else if(lchild == nullptr) return false; else if(rchild == nullptr) return false; if(lchild->val != rchild->val) return false; if(!dfs(lchild->left, rchild->right)) return false; if(!dfs(lchild->right, rchild->left)) return false; return true; } bool isSymmetric(TreeNode* root) { return dfs(root->left, root->right); } };
标签:right,TreeNode,nullptr,return,二叉树,对称,false,101,left From: https://www.cnblogs.com/WTSRUVF/p/16625931.html