File tree 3 files changed +104
-0
lines changed
3 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+ public boolean IsMeasure (TreeNode left , TreeNode right ){
18
+ if (left == null && right == null ){
19
+ return true ;
20
+ }
21
+ if (left == null && right != null ){
22
+ return false ;
23
+ }
24
+ if (right == null && left != null ){
25
+ return false ;
26
+ }
27
+
28
+ return left .val == right .val && IsMeasure (left .left , right .right ) && IsMeasure (left .right , right .left );
29
+ }
30
+
31
+ public boolean isSymmetric (TreeNode root ) {
32
+
33
+ if (root == null ){
34
+ return true ;
35
+ }
36
+
37
+ return IsMeasure (root .left , root .right );
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+ int counter = 0 ;
18
+ public int depth (TreeNode node ){
19
+ if (node == null ){
20
+ return 0 ;
21
+ }
22
+ int leftdepth = depth (node .left );
23
+ int rightdepth = depth (node .right );
24
+
25
+ if (leftdepth > rightdepth ){return leftdepth + 1 ;}
26
+ else {return rightdepth + 1 ;}
27
+
28
+ }
29
+
30
+ public int maxDepth (TreeNode root ) {
31
+ if (root == null ) return 0 ;
32
+ return depth (root );
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+ public int minDepth (TreeNode root ) {
18
+ if (root == null ) return 0 ;
19
+ int leftdepth = minDepth (root .left );
20
+ int rightdepth = minDepth (root .right );
21
+ if (leftdepth < rightdepth && leftdepth != 0 ){
22
+ return leftdepth + 1 ;
23
+ } else if (rightdepth < leftdepth && rightdepth != 0 ){
24
+ return rightdepth + 1 ;
25
+ } else if (rightdepth < leftdepth && rightdepth == 0 ){
26
+ return leftdepth + 1 ;
27
+ }
28
+ //if left depth < rightdepth and left depth == 0
29
+ return rightdepth + 1 ;
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments