LeetCode: 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.
思路:1、在判斷是否為映象的時候,如果採用層序遍歷就無法確定第二種情況不是映象樹,所以需要去觀察映象的特點,對問題進行分解,發現可以分解成為看兩個結點(左結點和右結點)進行遞迴,來判斷是否為映象;
2、注意起始條件的設定和結束的條件,是其兩個結點都是葉子結點的時候結束;
完整的程式碼:
public class Solution { public boolean isSymmetric(TreeNode root) { if(root==null) return true; return isEqual(root.left,root.right); } public boolean isEqual(TreeNode root1,TreeNode root2){ if(root1==null && root2==null) return true; if(root1==null || root2==null) return false; if(root1.val != root2.val) return false; return isEqual(root1.left,root2.right)&&isEqual(root1.right,root2.left);//關鍵程式碼,映象特點 } }
相關推薦
LeetCode: symmetric-tree判斷是否為映象樹
題目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For
[LeetCode] 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
leetcode Symmetric Tree 判斷是不是對稱樹
使用迭代方法: public boolean isSymmetric(TreeNode root) { if(root==null){ return true; } if(root!=null){ return leftAndRightTreeNode(root.left
【LeetCode】Symmetric Tree 判斷一棵樹是否是映象的
<span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 對稱的樹 * 思路:判斷一棵樹是否對稱,1.有左子樹就要有右子樹 *
leetcode 101. 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 symmetr
leetcode101. Symmetric Tree判斷一棵樹是否是映象二叉樹
思路:轉化為求兩個二叉樹是否是映象二叉樹class Solution { //判斷一顆二叉樹是否是映象的 public boolean isSymmetric(TreeNode root) { if(root==null){
LeetCode--Same Tree(判斷兩個二叉樹是否相同)Python
題目: 給定兩個二叉樹,判斷這兩個二叉樹是否完全相同。 解題思路: 直接通過中序遍歷、前序遍歷或者後續遍歷遍歷這兩棵二叉樹,得到兩個list結果,判斷這兩個list是否相同,相同返回True,否則返回False。需要注意考慮葉子節點的情況。 程式碼(Python): #
017-101-Symmetric Tree 判斷樹是否對稱
Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary
[leetcode]Same Tree(判斷兩個二叉樹是否相等 C語言實現)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if th
[LeetCode] Symmetric Tree
des lock 對稱 log and {} 對稱樹 script public Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Microsoft leetcode (Symmetric Tree)
turn des ack for fff pub ret treenode gpo Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun
LeetCode - Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :
[leetcode]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:
LeetCode-Symmetric Tree
Description: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ce
LeetCode Symmetric Tree
Problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this
leetcode -symmetric tree
\\採用遞迴方法,挺好理解的class Solution { public: bool isSame(TreeNode *left,TreeNode *right){ if((right==NULL&&left!=NULL)||(left==NU
[LeetCode] Binary Tree Upside Down 二叉樹的上下顛倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upsid
[LeetCode] Binary Tree Postorder Traversal 二叉樹的後序遍歷
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 /
[LeetCode] Binary Tree Inorder Traversal 二叉樹的中序遍歷
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3
[LeetCode] Binary Tree Preorder Traversal 二叉樹的先序遍歷
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3