Node
public class Node {
public int val;
public Node left;
public Node right;
public Node(int val) {
this.val = val;
}
public Node(int val, Node left, Node right) {
this.val = val;
this.left = left;
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"val=" + val +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return val == node.val &&
Objects.equals(left, node.left) &&
Objects.equals(right, node.right);
}
@Override
public int hashCode() {
return Objects.hash(val, left, right);
}
}
相关代码
import sun.plugin.viewer.LifeCycleManager;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Stack;
public class TestNode {
public void preOrder(Node node) {
if (node == null) {
return;
}
System.out.println(node);
preOrder(node.left);
preOrder(node.right);
}
public void order(Node node) {
if (node == null) {
return;
}
order(node.left);
System.out.println(node);
order(node.right);
}
public void lastOrder(Node node) {
if (node == null) {
return;
}
lastOrder(node.left);
lastOrder(node.right);
System.out.println(node);
}
public void preOrderUnRecur(Node node) {
if (node != null) {
Stack<Node> nodes = new Stack<>();
nodes.push(node);
while (!nodes.isEmpty()) {
node = nodes.pop();
System.out.println(node);
if (node.right != null) {
nodes.push(node.right);
}
if (node.left != null) {
nodes.push((node.left));
}
}
}
}
public void orderUnRecur(Node head) {
if (head != null) {
Stack<Node> nodes = new Stack<>();
while (!nodes.isEmpty() || head != null) {
if (head != null) {
nodes.push(head);
head = head.left;
} else {
head = nodes.pop();
System.out.println(head + " ");
head = head.right;
}
}
}
}
public int width(Node head) {
if (head == null) {
return 0;
}
LinkedList<Node> queue = new LinkedList<>();
queue.add(head);
HashMap<Node, Integer> map = new HashMap<>();
map.put(head, 1);
int curLevel = 1;
int curLevelNodes = 0;
int max = Integer.MIN_VALUE;
while (!queue.isEmpty()) {
head = queue.poll();
int curNodeLevel = map.get(head);
if (curLevel == curNodeLevel) {
curLevelNodes++;
} else {
max = Math.max(max, curLevelNodes);
curLevel++;
curLevelNodes = 1;
}
if (head.left != null) {
map.put(head.left, curNodeLevel + 1);
queue.add(head.left);
}
if (head.right != null) {
map.put(head.right, curNodeLevel + 1);
queue.add(head.right);
}
}
return Math.max(max, curLevelNodes);
}
public void lastOrderUnRecur(Node node) {
if (node != null) {
Stack<Node> node1 = new Stack<>();
Stack<Node> node2 = new Stack<>();
node1.push(node);
while (!node1.isEmpty()) {
node = node1.pop();
node2.push(node);
if (node.left != null) {
node1.push(node.left);
}
if (node.right != null) {
node1.push(node.right);
}
}
while (!node2.isEmpty()) {
System.out.println(node2.pop() + " ");
}
}
}
public boolean isBinarySearchTree(Node head) {
if (head != null) {
int preVal = Integer.MIN_VALUE;
Stack<Node> stack = new Stack<>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
if (head.val <= preVal) {
return false;
} else {
preVal = head.val;
}
head = head.right;
}
}
}
return true;
}
public static class ReturnDataOfSearch{
int max;
int min;
boolean isSearchBinaryTree;
public ReturnDataOfSearch(int max, int min, boolean isSearchBinaryTree) {
this.max = max;
this.min = min;
this.isSearchBinaryTree = isSearchBinaryTree;
}
}
public boolean isBinarySearchTree2(Node root){
return process3(root).isSearchBinaryTree;
}
private ReturnDataOfSearch process3(Node root) {
if (root == null){
return null;
}
ReturnDataOfSearch left = process3(root.left);
ReturnDataOfSearch right = process3(root.right);
boolean isBST = true;
if (left!=null && (!left.isSearchBinaryTree || left.max >= root.val)){
isBST = false;
}
if (right!=null&& (!right.isSearchBinaryTree|| right.min <= root.val)){
isBST = false;
}
int max = root.val;
int min = root.val;
if (left!=null){
min = Math.min(min,left.min);
max = Math.max(max,left.max);
}
if (right!=null){
min = Math.min(min,right.min);
max = Math.max(max,right.max);
}
return new ReturnDataOfSearch(max,min,isBST);
}
public Node lowestCommonAncestor(Node root, Node o1, Node o2) {
if (root == null || root == o1 || root == o2) {
return root;
}
Node left = lowestCommonAncestor(root.left, o1, o2);
Node right = lowestCommonAncestor(root.right, o1, o2);
if (left != null && right != null) {
return root;
}
return left != null ? left : right;
}
public boolean isCompleteBinaryTree(Node head) {
if (head != null) {
boolean leaf = false;
LinkedList<Node> nodes = new LinkedList<>();
nodes.add(head);
while (!nodes.isEmpty()) {
head = nodes.poll();
Node left = head.left;
Node right = head.right;
if (leaf && (left != null || right != null) || (left == null && right != null)) {
return false;
}
if (left != null) {
nodes.add(left);
}
if (right != null) {
nodes.add(right);
}
if (left == null || right == null) {
leaf = true;
}
}
}
return true;
}
public boolean isCompleteBinaryTree2(Node head) {
if (head != null) {
boolean flag = false;
LinkedList<Node> nodes = new LinkedList<>();
nodes.add(head);
while (!nodes.isEmpty()) {
head = nodes.poll();
if (head == null) {
flag = true;
} else {
if (flag) {
return false;
}
nodes.add(head.left);
nodes.add(head.right);
}
}
}
return true;
}
public static class Info {
int nodes;
int height;
public Info(int nodes, int height) {
this.nodes = nodes;
this.height = height;
}
}
public boolean isFullBinaryTree(Node root) {
if (root == null) {
return true;
}
Info info = process(root);
return info.nodes == ((1 << info.height) - 1);
}
private Info process(Node root) {
if (root == null) {
return new Info(0, 0);
}
Info leftData = process(root.left);
Info rightData = process(root.right);
int nodes = leftData.nodes + rightData.nodes + 1;
int height = Math.max(leftData.height, rightData.height) + 1;
return new Info(nodes, height);
}
public static class ReturnData{
int height;
boolean isBalance;
public ReturnData(int height, boolean isBalance) {
this.height = height;
this.isBalance = isBalance;
}
}
public boolean isBalanceBinaryTree(Node root){
return process2(root).isBalance;
}
private ReturnData process2(Node root) {
if (root == null){
return new ReturnData(0,true);
}
ReturnData leftData = process2(root.left);
ReturnData rightData = process2(root.right);
int height = Math.max(leftData.height,rightData.height)+1;
boolean isBalance = leftData.isBalance && rightData.isBalance && Math.abs(leftData.height-rightData.height) < 2;
return new ReturnData(height,isBalance);
}
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
node1.left = node2;
node2.left = node4;
node1.right = node3;
node2.right = node5;
node3.left = node6;
node3.right = node7;
TestNode testNode = new TestNode();
System.out.println("=======递归===========");
System.out.println("先序遍历");
testNode.preOrder(node1);
System.out.println("中序遍历");
testNode.order(node1);
System.out.println("后序遍历");
testNode.lastOrder(node1);
System.out.println("=======非递归===========");
System.out.println("先序遍历");
testNode.preOrderUnRecur(node1);
System.out.println("中序遍历");
testNode.orderUnRecur(node1);
System.out.println("后序遍历");
testNode.lastOrderUnRecur(node1);
System.out.println("宽度遍历:");
System.out.println(testNode.width(node1));
System.out.println("判断是否是搜索二叉树");
System.out.println(testNode.isBinarySearchTree(node1));
System.out.println("判断是否是完全二叉树");
System.out.println(testNode.isCompleteBinaryTree(node1));
System.out.println("判断是否是满二叉树");
System.out.println(testNode.isFullBinaryTree(node1));
System.out.println("判断是否是平衡二叉树");
System.out.println(testNode.isBalanceBinaryTree(node1));
}
}