Related topics of binary tree

Posted by RandomEngy on Fri, 12 Nov 2021 22:30:56 +0100

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) {
return true;
}
if (!isleftBST){
return false;
}
return false;
}else {
}
}
```
```public static boolean isBST3(Node head) {
int preValue = Integer.MIN_VALUE;
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
} else {
return false;
}else {
}
}
}
}
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) {
return true;
}
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;
while (!queue.isEmpty()) {
if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
return false;
}
if (l != null) {
}
if (r != null) {
} 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) {
}

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) {
}
Node left = lowestAncestor(head.left, o1, o2);
Node right = lowestAncestor(head.right, o1, o2);
if (left != null && right != null) {
}
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) {
return "#!";
}
String res = head.value + "!";
return res;
}

public static Node reconByPreString(String preStr) {
String[] values = preStr.split("!");
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;
}