【leetcode】P101SymmetricTree
遞迴,求一個二叉樹是否對稱,即是求左右子樹是否堆成,由此可以構建一個遞迴過程,以左右節點為引數,判斷是否堆成,要求左右節點值相等並且左右節點的孩子節點互為映象對稱節點
//給定一個二叉樹,檢查它是否是映象對稱的。
//
//
//
// 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。
//
// 1
// / \
// 2 2
// / \ / \
//3 4 4 3
//
//
//
//
// 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的:
//
// 1
// / \
// 2 2
// \ \
// 3 3
//
//
//
//
// 進階:
//
// 你可以運用遞迴和迭代兩種方法解決這個問題嗎?
// Related Topics 樹 深度優先搜尋 廣度優先搜尋
// ? 1102 ? 0
package leetcode.editor.cn;
//Java:對稱二叉樹
public class P101SymmetricTree {
public static void main(String[] args) {
P101SymmetricTree p101 = new P101SymmetricTree();
Solution solution = p101.new Solution();
TreeNode p = p101.new TreeNode(1);
p.left = p101.new TreeNode(2);
p.right = p101.new TreeNode(2);
p.left.left = p101.new TreeNode(3);
p.left.right = p101.new TreeNode(4);
p.right.left = p101.new TreeNode(4);
p.right.right = p101.new TreeNode(3);
boolean symmetric = solution.isSymmetric(p);
System.out.println(symmetric);
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
else return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode a, TreeNode b) {
if (a == null && b == null)
return true;
else if (a == null || b == null)
return false;
else if (a.val != b.val)
return false;
else if (!isSymmetric(a.left, b.right) || !isSymmetric(a.right, b.left))
return false;
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
迭代,基於佇列和層次遍歷的思路將遞迴改成迭代,每次從佇列取出兩個節點進行判斷,每次入隊時如一對映象位置的節點,比如將左樹左孩子和右數右孩子,左樹右孩子和右樹左孩子,只要root節點不為空,就將左右孩子節點入隊,判斷值是否相等,不等直接結束,返回false,相等則將兩棵樹的各自孩子映象入隊,佇列為空結束
//給定一個二叉樹,檢查它是否是映象對稱的。
//
//
//
// 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。
//
// 1
// / \
// 2 2
// / \ / \
//3 4 4 3
//
//
//
//
// 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的:
//
// 1
// / \
// 2 2
// \ \
// 3 3
//
//
//
//
// 進階:
//
// 你可以運用遞迴和迭代兩種方法解決這個問題嗎?
// Related Topics 樹 深度優先搜尋 廣度優先搜尋
// ? 1102 ? 0
package leetcode.editor.cn;
import java.util.LinkedList;
//Java:對稱二叉樹
public class P101SymmetricTree {
public static void main(String[] args) {
P101SymmetricTree p101 = new P101SymmetricTree();
Solution solution = p101.new Solution();
TreeNode p = p101.new TreeNode(1);
p.left = p101.new TreeNode(2);
p.right = p101.new TreeNode(2);
p.left.left = p101.new TreeNode(3);
p.left.right = p101.new TreeNode(4);
p.right.left = p101.new TreeNode(4);
p.right.right = p101.new TreeNode(3);
boolean symmetric = solution.isSymmetric(p);
System.out.println(symmetric);
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode a, TreeNode b) {
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(a);
queue.offer(b);
while (!queue.isEmpty()) {
a = queue.poll();
b = queue.poll();
if (a == null && b == null)
continue;
if (a == null || b == null)
return false;
if (a.val != b.val)
return false;
queue.offer(a.left);
queue.offer(b.right);
queue.offer(a.right);
queue.offer(b.left);
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}
相關文章
- 【LeetCode】如何學習LeetCode?LeetCode
- LeetcodeLeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- 我的 LeetCodeLeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode
- 嘗試 LeetcodeLeetCode
- LeetCode 消除遊戲LeetCode遊戲
- Leetcode03LeetCode
- Leetcode LRU CacheLeetCode
- Leetcode Path SumLeetCode
- LeetCode Min StackLeetCode
- Leetcode Word SearchLeetCode
- Leetcode Number of islandsLeetCode
- Leetcode-394LeetCode
- Leetcode 921 JavascriptLeetCodeJavaScript
- golang刷leetcodeGolangLeetCode
- LeetCode:快速排序LeetCode排序
- [LeetCode] Group AnagramLeetCode
- leetcode690LeetCode
- LeetCode 283 jsLeetCodeJS
- Leetcode #463 JavascriptLeetCodeJavaScript
- Leetcode # 495 JavascriptLeetCodeJavaScript
- Leetcode 513 javascriptLeetCodeJavaScript
- Leetcode725LeetCode
- Leetcode目錄LeetCode