Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 511a132

Browse files
committed
Symmetric Tree Recursive solution done
1 parent ac90ee6 commit 511a132

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.leetcode.trees;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
/**
6+
* Level: Easy
7+
* Problem Link: https://leetcode.com/problems/symmetric-tree/
8+
* Problem Description:
9+
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
10+
*
11+
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
12+
*
13+
* 1
14+
* / \
15+
* 2 2
16+
* / \ / \
17+
* 3 4 4 3
18+
*
19+
*
20+
* But the following [1,2,2,null,3,null,3] is not:
21+
*
22+
* 1
23+
* / \
24+
* 2 2
25+
* \ \
26+
* 3 3
27+
*
28+
*
29+
* Note:
30+
* Bonus points if you could solve it both recursively and iteratively.
31+
*
32+
* @author rampatra
33+
* @since 2019年07月25日
34+
*/
35+
public class SymmetricTree {
36+
37+
/**
38+
* Time Complexity: O(n) Because we traverse the entire input tree once, the total run time is O(n), where n is
39+
* the total number of nodes in the tree.
40+
* Space Complexity: O(n) The number of recursive calls is bound by the height of the tree. In the worst case, the
41+
* tree is linear and the height is in O(n). Therefore, space complexity due to recursive calls on the stack is
42+
* O(n) in the worst case.
43+
* Runtime: <a href="https://leetcode.com/submissions/detail/246324484/">0 ms</a>.
44+
*
45+
* @param root
46+
* @return
47+
*/
48+
public static boolean isSymmetric(TreeNode root) {
49+
if (root == null) {
50+
return true;
51+
}
52+
53+
return isSymmetric(root.left, root.right);
54+
}
55+
56+
private static boolean isSymmetric(TreeNode leftRoot, TreeNode rightRoot) {
57+
if (leftRoot == null && rightRoot == null) {
58+
return true;
59+
} else if (leftRoot == null || rightRoot == null) {
60+
return false;
61+
}
62+
63+
return isSymmetric(leftRoot.left, rightRoot.right) && isSymmetric(leftRoot.right, rightRoot.left) && leftRoot.val == rightRoot.val;
64+
}
65+
66+
public static void main(String[] args) {
67+
TreeNode root = new TreeNode(1);
68+
root.left = new TreeNode(2);
69+
root.right = new TreeNode(2);
70+
root.left.left = new TreeNode(4);
71+
root.left.right = new TreeNode(3);
72+
root.right.left = new TreeNode(3);
73+
root.right.right = new TreeNode(4);
74+
75+
assertTrue(isSymmetric(root));
76+
}
77+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.leetcode.trees;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019年07月25日
6+
*/
7+
public class TreeNode {
8+
9+
int val;
10+
TreeNode left;
11+
TreeNode right;
12+
13+
public TreeNode(int val) {
14+
this.val = val;
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /