Skip to content

Commit 9122055

Browse files
authored
Merge pull request gzc426#313 from PYTWSW/master
create WhiteNight.md
2 parents e4b897b + ab68ccf commit 9122055

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

2018.12.04-leetcode101/WhiteNight.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
>```java
2+
>/**
3+
> *给定一个二叉树,检查它是否是镜像对称的。
4+
> *
5+
> */
6+
>public class Tree9
7+
>{
8+
> public static class TreeNode
9+
> {
10+
> int data;
11+
> TreeNode left;
12+
> TreeNode right;
13+
>
14+
> TreeNode(int val) {
15+
> data = val;
16+
> }
17+
> }
18+
>
19+
> public boolean isSymmetric(TreeNode root)
20+
> {
21+
> if (root == null)
22+
> return true;
23+
> return checkIsSymmetric(root.left, root.right);
24+
> }
25+
>
26+
> public static boolean checkIsSymmetric(TreeNode leftNode, TreeNode rightNode)
27+
> {
28+
> if (leftNode == null && rightNode == null)
29+
> return true;
30+
> if ((leftNode == null && rightNode != null) || (leftNode != null && rightNode == null))
31+
> return false;
32+
> if (leftNode.data != rightNode.data)
33+
> return false;
34+
> return checkIsSymmetric(leftNode.left, rightNode.right) && checkIsSymmetric(leftNode.right, rightNode.left);
35+
> }
36+
>
37+
> public static void main(String[] args)
38+
> {
39+
> TreeNode p = new TreeNode(1);
40+
> p.left = new TreeNode(2);
41+
> p.right = new TreeNode(2);
42+
> p.left.left = new TreeNode(3);
43+
> p.right.right = new TreeNode(3);
44+
>
45+
> TreeNode q = new TreeNode(1);
46+
> q.left = new TreeNode(2);
47+
> q.right = new TreeNode(2);
48+
> q.left.right = new TreeNode(3);
49+
> q.right.right = new TreeNode(3);
50+
>
51+
> Tree9 t = new Tree9();
52+
> System.out.println(t.isSymmetric(p));
53+
> System.out.println(t.isSymmetric(q));
54+
>
55+
> }
56+
>
57+
>}
58+
>```

2018.12.3-leetcode58/WhiteNight.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
>```java
2+
>/**
3+
> * leetcode58 最后一个单词的长度
4+
> *
5+
> */
6+
>public class S5 {
7+
> public int lengthOfLastWord(String s) {
8+
> String[] string = s.split(" ");
9+
> if (string == null || string.length == 0)
10+
> return 0;
11+
> int i = string.length - 1;
12+
> return string[i].length();
13+
> }
14+
>
15+
> public static void main(String[] args) {
16+
> S5 s = new S5();
17+
> String string = "Hello World";
18+
> int res = s.lengthOfLastWord(string);
19+
> System.out.println(res);
20+
> }
21+
>}
22+
>```
23+

0 commit comments

Comments
 (0)