Movatterモバイル変換


[0]ホーム

URL:


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

Commit81e067a

Browse files
author
applewjg
committed
SymmetricTree
Change-Id: Ib4dece75b87b545c5f1a1fde03ce54717a3c912a
1 parentf0aed18 commit81e067a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎SymmetricTree.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Author: King, higuige@gmail.com
3+
Date: Dec 25, 2014
4+
Problem: Symmetric Tree
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/symmetric-tree/
7+
Notes:
8+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
9+
For example, this binary tree is symmetric:
10+
1
11+
/ \
12+
2 2
13+
/ \ / \
14+
3 4 4 3
15+
But the following is not:
16+
1
17+
/ \
18+
2 2
19+
\ \
20+
3 3
21+
Note:
22+
Bonus points if you could solve it both recursively and iteratively.
23+
24+
Solution: 1. Recursive solution
25+
*/
26+
27+
/**
28+
* Definition for binary tree
29+
* public class TreeNode {
30+
* int val;
31+
* TreeNode left;
32+
* TreeNode right;
33+
* TreeNode(int x) { val = x; }
34+
* }
35+
*/
36+
publicclassSolution {
37+
publicbooleanisSymmetric(TreeNoderoot) {
38+
if (root ==null)returntrue;
39+
returnsolve (root.left,root.right);
40+
}
41+
publicbooleansolve(TreeNodet1,TreeNodet2) {
42+
if (t1 ==null &&t2 ==null)returntrue;
43+
if (t1 ==null &&t2 !=null ||t1 !=null &&t2 ==null ||t1.val !=t2.val)returnfalse;
44+
returnsolve(t1.left,t2.right) &&solve(t1.right,t2.left);
45+
}
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp