1publicbooleanisSymmetricTree(TreeNode p,TreeNode q){ 2if(p ==null&&q ==null) 3returntrue; 4if(p ==null||q ==null) 5returnfalse; 6return(p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left); 7} 8 9publicbooleanisSymmetric(TreeNode root) ...
TreeNode(intx) { val = x; } } 算法实现类 publicclassSolution{publicboolean isSymmetric(TreeNode root) {if(root ==null) {returntrue; }else{returnisSame(root.left, root.right); } }privateboolean isSame(TreeNode left, TreeNode right) {if(left ==null&& right ==null) {returntrue; }if...
1/**2* Definition for binary tree3* public class TreeNode {4* int val;5* TreeNode left;6* TreeNode right;7* TreeNode(int x) { val = x; }8* }9*/10publicclassSolution {11publicbooleanisSymmetric(TreeNode root) {12if(root ==null)returntrue;13returncheck(root.left, root.right);1...
} public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null || q == null) { return p == q; } boolean f = p.val == q.val; boolean f2 = isSameTree(p.left, q.right); boolean f3 = isSameTree(p.right, q.left); return f && f2 && f3; } 1. 2. 3. 4. ...
Binary Tree Tilt (Python) 36 -- 4:58 App Leetcode 476. Number Complement (Python) 24 -- 5:53 App Leetcode 1200. Minimum Absolute Difference (Python) 24 -- 5:10 App Leetcode 849. Maximize Distance to Closest Person (Python) 83 -- 5:16 App LeetCode 795. Number of Subarrays...
#LeetCode# I have solved Symmetric Tree. Come and join the fun! http://t.cn/RUsuCVV
101. 对称二叉树 - 给你一个二叉树的根节点 root , 检查它是否轴对称。 示例 1: [https://pic.leetcode.cn/1698026966-JDYPDU-image.png] 输入:root = [1,2,2,3,4,4,3] 输出:true 示例 2: [https://pic.leetcode.cn/1698027008-nPFLbM-image.png] 输入:ro
101. 对称二叉树 - 给你一个二叉树的根节点 root , 检查它是否轴对称。 示例 1: [https://pic.leetcode.cn/1698026966-JDYPDU-image.png] 输入:root = [1,2,2,3,4,4,3] 输出:true 示例 2: [https://pic.leetcode.cn/1698027008-nPFLbM-image.png] 输入:ro
* TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classSolution{public:booldfs(TreeNode*t1,TreeNode*t2){if(!t1||!t2){if(!t1&&!t2)returntrue;returnfalse;}if(t1->val!=t2->val)returnfalse;returndfs(t1->left,t2->right)&&dfs(t1->right,t2-...
Can you solve this real interview question? Symmetric Tree - Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example 1: [https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg] Inpu