这个方法略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; }
相关推荐
* [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]...
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-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 - ...
leetcode分发糖果 Leetcode C++ Solution Don't try to understand it, feel ...21-合并两个有序链表:merge-two-sorted-lists 83-删除排序链表中的重复元素:remove-duplicates-from-sorted-...101-对称二叉树:symmetric-
tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param { TreeNode } root * @return { boolean } */ var isSymmetric = function ( root ) { if ( root ...
Symmetric Tree Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal Construct ...
对称的二叉树(递归,清晰图解)# Definition for a binary tree node.def isSymmetric(self, root: T