本文主要是介绍Leetcode 101 Symmetric Tree(水),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目连接:Leetcode 101 Symmetric Tree
解题思路:递归判定,当前值需要相同,a的左子树与b的右子树对称,a的右子树与b的左子树对称。
/*** 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 symmetric(TreeNode* first, TreeNode* second) {if (first == NULL && second == NULL) return true;if (first == NULL || second == NULL) return false;if (first->val != second->val) return false;return symmetric(first->left, second->right) && symmetric(first->right, second->left);}bool isSymmetric(TreeNode* root) {return symmetric(root, root);}
};
这篇关于Leetcode 101 Symmetric Tree(水)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!