package t;
/**
* <pre>
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*
* For example, this binary tree is symmetric:
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
* But the following is not:
* 1
* / \
* 2 2
* \ \
* 3 3
* </pre>
* */
public class SymmetricTree {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null)
return true;
if (left == null || right == null)
return false;
if (left.val != right.val)
return false;
return isSymmetric(left.left, right.right)
&& isSymmetric(left.right, right.left);
}
}
}
分享到:
相关推荐
java java_leetcode-101-symmetric-tree
python python_leetcode题解之101_Symmetric_Tree
js js_leetcode题解之101-symmetric-tree.js
在LeetCode上,第101题被称为“对称二叉树”(Symmetric Tree)。这是一道数据结构与算法问题,主要考察的是二叉树的遍历和比较。题目要求编写一个函数,判断给定的二叉树是否是对称的。对称二叉树的定义是:如果一...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
例如,树的层级遍历(Levelorder Traversal)、判断树的对称性(Symmetric Tree)、找到二叉搜索树中距离某个值最近的节点(Closest Binary Search Tree Value)等。这些题目通常要求编写者熟悉树的结构和遍历方法,...
- **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树是否是平衡的。 - **Path Sum**:判断是否存在一条路径,其节点值...
* Symmetric Tree:给定一个二叉树,判断是否为对称树。这个题目需要使用递归的思想,将树分解成更小的子树,并判断是否对称。 4. 动态规划 动态规划是一种非常重要的算法思想,LeetCode 中有很多关于动态规划的...
Symmetric Tree)。这两个问题都涉及到对二叉树结构的深入理解和遍历策略。 首先,我们来看验证二叉搜索树。二叉搜索树(Binary Search Tree, BST)是一种特殊的二叉树,其特性是每个节点的值大于其左子树中的所有...
leetcode 答案 LeetCode-Trip ...Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
isSymmetric(TreeNode root) { if(root == NULL) return true; return checkSymmetric(root->left, root->right); } bool checkSymmetric(TreeNode* left, TreeNode* right) { if(left == NULL && right == NULL) ...
lru缓存leetcode ...https://leetcode.com/problems/symmetric-tree/ Symmetric Tree 102 https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree Level Order Traversal 103 ...
leetcode 树节点leetcode 测试 仅使用适用于python 方便本地测试,ListNode和TreeNode类型 # filename leetcode.py from leetcode_test ...isSymmetric(self, ...symmetric(left, ...tree = TreeNode.create
101.symmetric-tree (对称二叉树) 102.binary-tree-level-order-traversal (二叉树的层序遍历) 104.maximum-depth-of-binary-tree (二叉树的最大深度) 105.construct-binary-tree-from-preorder-and-inorder-...
- **对称树(Symmetric Tree)**: 判断一个二叉树是否是对称的。 - **相同的树(Same Tree)**: 判断两个二叉树是否相同。 - **平衡二叉树(Balanced Binary Tree)**: 判断一个二叉树是否是高度平衡的。 ##### 动态规划...
Symmetric tree - Java Recursive - Java Iterative - C Recursive - Python Iterative 0102 Binary Tree Level Order Traversal - Python3 iterative 0103 Binary Tree Zigzag Level Order Traversal - ...
leetcode分发糖果 Leetcode C++ Solution Don't try to understand it, feel ...21-合并两个有序链表:merge-two-sorted-lists 83-删除排序链表中的重复元素:remove-duplicates-from-sorted-...101-对称二叉树:symmetric-
3. **二叉树**:二叉树问题是算法题目的重头戏,包括"二叉树的遍历"(Traversal)、"判断二叉树对称性"(Symmetric Tree)等。Swift中的可选类型(Optional)使得处理空节点变得直观。 4. **动态规划**:动态规划是...
tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param { TreeNode } root * @return { boolean } */ var isSymmetric = function ( root ) { if ( root ...
1. 每个文件对应一个LeetCode题目,文件名可能包含了题目的ID或者描述,比如"001-two-sum.js"或"101-symmetric-tree.cpp"。 2. 文件内容通常包括了问题的解法,可能有详细的注释来解释思路和关键步骤。 3. 解决方案...