LeetCode: Symmetric Tree 解题报告

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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

SOL 1 & 2:

两个解法,递归和非递归.

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     // solution 1:
12     public boolean isSymmetric(TreeNode root) {
13         if (root == null) {
14             return true;
15         }
16
17         return isSymmetricTree(root.left, root.right);
18     }
19
20     /*
21      * 判断两个树是否互相镜像
22         (1) 根必须同时为空,或是同时不为空
23      *
24      * 如果根不为空:
25         (1).根的值一样
26         (2).r1的左树是r2的右树的镜像
27         (3).r1的右树是r2的左树的镜像
28      */
29     public boolean isSymmetricTree1(TreeNode root1, TreeNode root2) {
30         if (root1 == null && root2 == null) {
31             return true;
32         }
33
34         if (root1 == null || root2 == null) {
35             return false;
36         }
37
38         return root1.val == root2.val &&
39              isSymmetricTree(root1.left, root2.right) &&
40              isSymmetricTree(root1.right, root2.left);
41     }
42
43     // solution 2:
44     public boolean isSymmetricTree(TreeNode root1, TreeNode root2) {
45         if (root1 == null && root2 == null) {
46             return true;
47         }
48
49         if (root1 == null || root2 == null) {
50             return false;
51         }
52
53         Stack<TreeNode> s1 = new Stack<TreeNode>();
54         Stack<TreeNode> s2 = new Stack<TreeNode>();
55
56         // 一定记得初始化
57         s1.push(root1);
58         s2.push(root2);
59
60         while (!s1.isEmpty() && !s2.isEmpty()) {
61             TreeNode cur1 = s1.pop();
62             TreeNode cur2 = s2.pop();
63
64             if (cur1.val != cur2.val) {
65                 return false;
66             }
67
68             if (cur1.left != null && cur2.right != null) {
69                 s1.push(cur1.left);
70                 s2.push(cur2.right);
71             } else if (!(cur1.left == null && cur2.right == null)) {
72                 return false;
73             }
74
75             if (cur1.right != null && cur2.left != null) {
76                 s1.push(cur1.right);
77                 s2.push(cur2.left);
78             } else if (!(cur1.right == null && cur2.left == null)) {
79                 return false;
80             }
81         }
82
83         return true;
84     }
85 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSymmetric.java

时间: 2024-10-12 11:51:10

LeetCode: Symmetric Tree 解题报告的相关文章

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

Leetcode:Symmetric Tree 判断对称树

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 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

LeetCode: Symmetric Tree [101]

[题目] 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 bot

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

Microsoft leetcode (Symmetric Tree)

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / 2 2 \

【LeetCode】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归

LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between t