Related topics of binary tree
Related concepts of binary tree and its implementation judgment
How to judge whether a tree is a search binary tree?
Search Binary Tree
For each subtree, it is satisfied. The left tree is smaller than the node, and the right tree is larger than the node. That is to search the binary tree.
Judge whether the middle order traversal is in ascending order. If so, it is to search the binary tree
public static int preValue = Integer.MIN_VALUE; public static boolean isBST(Node head) { if (head == null) { return true; } boolean isleftBST = isBST(head.left); if (!isleftBST){ return false; } if (head.value <= preValue) { return false; }else { preValue = head.value; } return isBST(head.right); }
public static boolean isBST3(Node head) { if (head != null) { int preValue = Integer.MIN_VALUE; Stack<Node> stack = new Stack<Node>(); while (!stack.isEmpty() || head != null) { if (head != null) { stack.push(head); head = head.left; } else { head = stack.pop(); if (head.value <= preValue) { return false; }else { preValue = head.value; } head = head.right; } } } return true; }
Judge whether it is a complete binary tree
Traverse by width
1) If there are right children and no left children, false is returned.
2) If the first condition does not exist and the first left and right children are incomplete, then all the next nodes must be leaf nodes. Otherwise false
public static boolean isCBT(Node head) { if (head == null) { return true; } LinkedList<Node> queue = new LinkedList<>(); boolean leaf = false; //Indicates whether the left and right children's incomplete events have occurred. It has been true since they occurred Node l = null; Node r = null; queue.add(head); while (!queue.isEmpty()) { head = queue.poll(); l = head.left; r = head.right; if ((leaf && (l != null || r != null)) || (l == null && r != null)) { return false; } if (l != null) { queue.add(l); } if (r != null) { queue.add(r); } else { leaf = true; } } return true; }
Judgment is a full binary tree
Statistical maximum depth D
Count the number of nodes N
If N = 2^D -1, then it is a full binary tree
Judgment is a balanced binary tree
For any subtree, the height difference between the left tree and the right tree cannot exceed 1.
Then its left subtree and right subtree are also balanced binary trees.
Several conditions are true, that is, balanced binary tree
public static boolean isBalanced(Node head) { return process(head).isBalanced; } public static class ReturnType { public boolean isBalanced; public int height; public ReturnType(boolean isB, int hei) { isBalanced = isB; height = hei; } } public static ReturnType process(Node x) { if (x == null) { return new ReturnType(true, 0); } ReturnType leftData = process(x.left); ReturnType rightData = process(x.right); int height = Math.max(leftData.height, rightData.height) + 1; boolean isBalanced = leftData.isBalanced && rightData.isBalanced && Math.abs(leftData.height - rightData.height) < 2; return new ReturnType(isBalanced, height); }
For tree DP problems, recursive routines can be used. You can usually do this for general interview questions, Problems that cannot be optimized are generally not tested.
Lowest common ancestor node
Given the nodes node1 and node2 of two binary trees, find their lowest common ancestor node. That is, the initial convergence point upward.
- Generate an upward chain. The parent node information chain of a node
- Find the common ancestor node when traversing
public static Node lowestAncestor(Node head, Node o1, Node o2) { if (head == null || head == o1 || head == o2) { return head; } Node left = lowestAncestor(head.left, o1, o2); Node right = lowestAncestor(head.right, o1, o2); if (left != null && right != null) { return head; } return left != null ? left : right; }
Successor node
The next node of a node in the middle order traversal is called its successor node
Predecessor node
[title] there is a new binary tree node type as follows:
public class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int val) {
value = val;
}
}
This structure has one more parent pointer to the parent Node than the ordinary binary tree Node structure. Suppose there is a binary tree composed of nodes of Node type. The parent pointer of each Node in the tree correctly points to its parent Node, and the parent of the head Node points to null.
Only one node in the binary tree is given. Please implement the function that returns the successor node of the node.
In the middle order traversal sequence of binary tree, the next node of node is called the successor node of node.
- If x has a right tree, then the left is its successor
- If there is no right tree, go up until you are a left child, and this ancestor node is the successor. But if you have not been a left child, then its successor is null
serialization and deserialization
It is how a tree in memory becomes a string, and how it changes from a string to a tree in memory
How to judge whether a binary tree is a subtree of another binary tree?
You can specify to create a left subtree first and then a right subtree.
public static String serialByPre(Node head) { if (head == null) { return "#!"; } String res = head.value + "!"; res += serialByPre(head.left); res += serialByPre(head.right); return res; } public static Node reconByPreString(String preStr) { String[] values = preStr.split("!"); Queue<String> queue = new LinkedList<String>(); for (int i = 0; i != values.length; i++) { queue.offer(values[i]); } return reconPreOrder(queue); } public static Node reconPreOrder(Queue<String> queue) { String value = queue.poll(); if (value.equals("#")) { return null; } Node head = new Node(Integer.valueOf(value)); head.left = reconPreOrder(queue); head.right = reconPreOrder(queue); return head; }