mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 11:06:28 +08:00
89 lines
2.1 KiB
Java
Executable file
89 lines
2.1 KiB
Java
Executable file
/*
|
|
* File: TreeNode.java
|
|
* Created Time: 2022-11-25
|
|
* Author: Krahets (krahets@163.com)
|
|
*/
|
|
|
|
package include;
|
|
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
*/
|
|
public class TreeNode {
|
|
public int val;
|
|
public TreeNode left;
|
|
public TreeNode right;
|
|
|
|
public TreeNode(int x) {
|
|
val = x;
|
|
}
|
|
|
|
/**
|
|
* Generate a binary tree with an array
|
|
* @param arr
|
|
* @return
|
|
*/
|
|
public static TreeNode arrToTree(Integer[] arr) {
|
|
if (arr.length == 0)
|
|
return null;
|
|
|
|
TreeNode root = new TreeNode(arr[0]);
|
|
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
|
int i = 1;
|
|
while(!queue.isEmpty()) {
|
|
TreeNode node = queue.poll();
|
|
if(arr[i] != null) {
|
|
node.left = new TreeNode(arr[i]);
|
|
queue.add(node.left);
|
|
}
|
|
i++;
|
|
if(arr[i] != null) {
|
|
node.right = new TreeNode(arr[i]);
|
|
queue.add(node.right);
|
|
}
|
|
i++;
|
|
}
|
|
return root;
|
|
}
|
|
|
|
/**
|
|
* Serialize a binary tree to a list
|
|
* @param root
|
|
* @return
|
|
*/
|
|
public static List<Integer> treeToList(TreeNode root) {
|
|
List<Integer> list = new ArrayList<>();
|
|
if(root == null) return list;
|
|
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
|
while(!queue.isEmpty()) {
|
|
TreeNode node = queue.poll();
|
|
if(node != null) {
|
|
list.add(node.val);
|
|
queue.add(node.left);
|
|
queue.add(node.right);
|
|
}
|
|
else {
|
|
list.add(null);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* Get a tree node with specific value in a binary tree
|
|
* @param root
|
|
* @param val
|
|
* @return
|
|
*/
|
|
public static TreeNode getTreeNode(TreeNode root, int val) {
|
|
if (root == null)
|
|
return null;
|
|
if (root.val == val)
|
|
return root;
|
|
TreeNode left = getTreeNode(root.left, val);
|
|
TreeNode right = getTreeNode(root.right, val);
|
|
return left != null ? left : right;
|
|
}
|
|
}
|