-
Notifications
You must be signed in to change notification settings - Fork 22
/
BinaryTree.java
54 lines (43 loc) · 1.18 KB
/
BinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.company.amazon;
public class BinaryTree {
public Node root;
static class Node {
int data;
Node left;
Node right;
int prevData;
Node() {
}
Node(int data) {
this.data = data;
}
}
public static void inorder(Node root) {
if (root == null)
return;
inorder(root.left);
System.out.print(root.data + ",");
inorder(root.right);
}
public static void inorder(Node root, Boolean printLogs) {
if (printLogs) {
System.out.println("=============Inorder =============");
}
inorder(root);
if (printLogs) {
System.out.println("\n=============Inorder END=============");
}
}
public static boolean isLeafNode(Node root) {
return root.left == null && root.right == null;
}
public static boolean hasBothChild(Node root) {
return root.left != null && root.right != null;
}
public static boolean hasRightChild(Node root) {
return root.right != null;
}
public static boolean hasLeftChild(Node root) {
return root.left != null;
}
}