这个方法略2,新博文地址:[leetcode]Symmetric Tree
http://oj.leetcode.com/problems/symmetric-tree/
写道
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
Note:
Bonus points if you could solve it both recursively and iteratively.
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
判断树是否是对称的。要求用递归和循环实现。
我对这道题大概有三种思想:
1. 层次遍历,判断每一层是否是对称的(循环思想)
2. 生成该树的镜像,判断是否跟其镜像相同(麻烦点,树的镜像和判断两棵树是否相同两个知识点)
3. 递归实现(网上基本上都是递归算法,代码我也懒得写了,直接粘过来了)
我写的是层次遍历的思想,比较直观,但是代码比较乱
public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } Queue<TreeNode> queue = new ArrayDeque<TreeNode>(); queue.offer(root); int numCount = 1;//表示当前层的节点数 while (!queue.isEmpty()) { String[] sList = new String[numCount * 2];//存储该层的节点val int index = 0; int levelNodeCount = 0;//临时变量,记录下一层的节点数 for (int i = 0; i < numCount; i++) { TreeNode node = queue.poll(); if (node.left != null) { queue.offer(node.left); levelNodeCount++; sList[index++] = String.valueOf(node.left.val); } else { sList[index++] = "#"; } if (node.right != null) { queue.offer(node.right); levelNodeCount++; sList[index++] = String.valueOf(node.right.val); } else { sList[index++] = "#"; } } numCount = levelNodeCount; index --;//sList的长度 for(int i = 0; i <= index /2; i++){//判断该层节点是否对称 if(!sList[i].equals(sList[index - i])){ return false; } } } return true; }
相关推荐
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]...
- **Symmetric Tree**:判断一个二叉树是否是对称的。 - **Same Tree**:判断两棵二叉树是否相同。 - **Balanced Binary Tree**:判断一个二叉树是否是平衡的。 - **Path Sum**:判断是否存在一条路径,其节点值...
例如,树的层级遍历(Levelorder Traversal)、判断树的对称性(Symmetric Tree)、找到二叉搜索树中距离某个值最近的节点(Closest Binary Search Tree Value)等。这些题目通常要求编写者熟悉树的结构和遍历方法,...
* Symmetric Tree:给定一个二叉树,判断是否为对称树。这个题目需要使用递归的思想,将树分解成更小的子树,并判断是否对称。 4. 动态规划 动态规划是一种非常重要的算法思想,LeetCode 中有很多关于动态规划的...
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) ...
Symmetric Tree)。这两个问题都涉及到对二叉树结构的深入理解和遍历策略。 首先,我们来看验证二叉搜索树。二叉搜索树(Binary Search Tree, BST)是一种特殊的二叉树,其特性是每个节点的值大于其左子树中的所有...
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-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]
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 - Java Recursive - Java Iterative - C Recursive - Python Iterative 0102 Binary Tree Level Order Traversal - Python3 iterative 0103 Binary Tree Zigzag Level Order Traversal - ...
- **对称树(Symmetric Tree)**: 判断一个二叉树是否是对称的。 - **相同的树(Same Tree)**: 判断两个二叉树是否相同。 - **平衡二叉树(Balanced Binary Tree)**: 判断一个二叉树是否是高度平衡的。 ##### 动态规划...
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 ...