目录
1、树的概念
结点的度 | 1个结点含有的子树的个数 |
树的度 | 所有结点度的最大值 |
叶子结点 | 度为0的结点 |
父结点 | 该结点的上一级 |
子结点 | 该结点的下一级 |
根结点 | 没有双亲结点的结点 |
结点的层次 | 树的高 |
树的高度 | 结点的最大层次 |
2、树的表现形式
class TreeNode{
int value;
TreeNode left;
TreeNode right;
}
3、二叉树
二叉树结点的度都是<=2的,且二叉树是有序树。
注意两种特殊的二叉树:第一种是满二叉树,即满结点。第二种是完全二叉树,序号是从上到小、从左到右排列的有序的。
4、二叉树的性质(重点)
1.若规定根节点的层数为1,则一棵非空二叉树的第i层上最多有个结点。
2.若规定只有根结点的二叉树深度为1,则深度为k的二叉树最大结点数为.
3.对于任何一个二叉树,n0=n2+1。n0为叶子结点,n2为度为2的结点。
4.具有n个结点的完全二叉树的深度k为向上取整。
5.对于具有n个结点的完全二叉树,如果按照从上至下、从左到右的顺序编号。则对于序号i的结点来说:
若i>0,双亲序号:(i-1)/2;i=0,i为根节点编号,无双亲结点。
若2i+1<n,左孩子序号:2i+1,否则无左孩子。
若2i+2<n,右孩子序号:2i+2,否则无右孩子。
5、二叉树的遍历
前序遍历:根左右
中序遍历:左根右
后续遍历:左右根
//前序遍历
void preOrder(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
//中序遍历
void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
//后序遍历
void postOrder(TreeNode root) {
if (root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val + " ");
}
6、层序遍历
void levelOrder(TreeNode root) {
if (root == null) {
return;
}
//遍历每一层的结点,将其存入到队列当中
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
//第一次手动加一个根
//一直遍历到queue中全是null为止
while (!queue.isEmpty()) {
//将当前queue中的元素打印一个出来
TreeNode cur = queue.poll();
System.out.print(cur.val + " ");
//如果cur.left不是null,就加入到queue中,
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
7、二叉树的基本操作
//获取树中结点的个数
int size(TreeNode root) {
if (root == null) {
return 0;
}
return size(root.left) + size(root.right) + 1;
}
//获得叶子结点个数
int getLeafNodeCount(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
}
//获取第K层节点的个数
int getKLevelNodeCount(TreeNode root, int k) {
//获得第k层结点的个数,为k-1层的左节点个数 + k-1层的右节点个数
if (root == null) {
return 0;
}
if (k == 1) {
return 1;
}
return getKLevelNodeCount(root.left, k-1) + getKLevelNodeCount(root.right, k-1);
}
// 获取二叉树的高度
int getHeight(TreeNode root) {
//左右子树的max高度+1
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
// 检测值为value的元素是否存在
boolean find(TreeNode root, int value) {
if (root == null) {
return false;
}
if (root.val == value) {
return true;
}
boolean left = find(root.left, value);
if (left) {
return true;
}
boolean right = find(root.right, value);
if (right) {
return true;
}
return false;
}
// 判断一棵树是不是完全二叉树
boolean isCompleteTree(TreeNode root) {
//通过不断的将树中的元素输出到队列中
//再将队列中的元素输出,当全部输出完毕以后,对队列中的元素进行判断
//如果有非空元素,则表示不是完全二叉树。如果全是null,则表示是完全二叉树
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
//1.存入queue,取出queue
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur != null) {
queue.offer(cur.left);
queue.offer(cur.right);
} else {
break;
}
}
//2.直到取出的cur是null,再停下来做判断,判断此时的queue是不是全为空
while (!queue.isEmpty()) {
TreeNode tmp = queue.peek();
if (tmp == null) {
queue.poll();
} else {
return false;
}
}
return true;
}
8、二叉树OJ问题
//检查两棵树是否相同
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q != null || p != null && q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
if (p == null && q == null) {
return true;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
//判断一棵树是不是另一棵树的子树
public boolean isSubtree(TreeNode p,TreeNode q){
if (p == null && q == null) {
return false;
}
if (isSameTree(p,q)){
return true;
}
if (isSubtree(p.left,q)){
return true;
}
if (isSubtree(p.right,q)){
return true;
}
return false;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q != null || p != null && q == null) {
return false;
}
if (p.val != q.val) {
return false;
}
if (p == null && q == null) {
return true;
}
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
//翻转二叉树
public TreeNode invertTree(TreeNode root){
if (root==null){
return null;
}
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
invertTree(root.left);
invertTree(root.right);
return root;
}
//判断一颗二叉树是否是平衡二叉树
public boolean isBalanced(TreeNode root){
if (root==null){
return false;
}
//获得当前root的左右高度
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
//return true 的唯一情况就是(高度差<=1)(左树平衡)(右树平衡)
return Math.abs(leftHeight-rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
//对称二叉树
public boolean isSymmetric(TreeNode root){
if (root==null){
return false;
}
return isSymmetricChild(root.left,root.right);
}
public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
if (leftTree == null && rightTree != null || leftTree != null && rightTree == null) {
return false;
}
if (leftTree == null && rightTree == null) {
return true;
}
return isSymmetricChild(leftTree.left, rightTree.right) &&
isSymmetricChild(leftTree.right, rightTree.left);
}
//二叉树的构建及遍历
class treeNode{
public char val;
public treeNode left;
public treeNode right;
public treeNode(char val){
this.val = val;
}
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int i;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
treeNode root = createNode(str);
inOrder(root);
}
}
public static treeNode createNode(String str){
//.1遍历字符串str
treeNode root = null;
if (str.charAt(i) != '#'){
//2.根据前序遍历创建二叉树
root = new treeNode(str.charAt(i));
i++;
root.left = createNode(str);
root.right = createNode(str);
}else{
i++;
}
//3.最后返回根结点
return root;
}
public static void inOrder(treeNode root){
if (root == null){
return ;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
}
//二叉树的分层遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if(root == null){
return ret;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
//得到当前队列有多大
//通过记录每次当前队列的size数,来统计每一层的list有多少个元素,
//从queue中出同样的多数量的元素到list中即可
int size = queue.size();
List<Integer> tmp = new ArrayList<>();
while (size != 0) {
//打印输出queue里的值
TreeNode cur = queue.poll();
tmp.add(cur.val);
size--;
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
ret.add(tmp);
}
return ret;
}
//给定一个二叉树, 找到该树中两个指定节点的最近公共祖先
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
if (root == null){
return null;
}
if (root == p || root == q){
return root;
}
TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
if (leftTree != null && rightTree != null){
return root;
} else if (leftTree != null) {
return leftTree;
} else if (rightTree != null) {
return rightTree;
}
return null;
}
public TreeNode lowestCommonAncestor2(TreeNode root,TreeNode p,TreeNode q){
if (root==null) return null;
Stack<TreeNode> stackP = new Stack<>();
Stack<TreeNode> stackQ = new Stack<>();
getPath(root,p,stackP);
getPath(root,q,stackQ);
int sizeP = stackP.size();
int sizeQ = stackQ.size();
if (sizeP > sizeQ){
int size = sizeP - sizeQ;
while (size != 0){
stackP.pop();
size--;
}
}else {
int size = sizeQ - sizeP;
while (size != 0){
stackQ.pop();
size--;
}
}
while (!stackP.isEmpty() && !stackQ.isEmpty()){
if (stackP.peek() == stackQ.peek()){
return stackP.peek();
}else {
stackP.pop();
stackQ.pop();
}
}
return null;
}
//根据一棵树的前序遍历与中序遍历构造二叉树
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeChild(preorder, inorder, 0, inorder.length - 1);
}
private TreeNode buildTreeChild(int[] preorder,int[] inorder,int inbegin,int inend){
if (inbegin > inend){
return null;//没有左树或者右树
}
TreeNode root = new TreeNode(preorder[preIndex]);
int rootIndex = findIndexRoot(inorder,inbegin,inend,preorder[preIndex]);
if (rootIndex == -1){
return null;
}
preIndex++;
root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex-1);
root.right = buildTreeChild(preorder,inorder,rootIndex+1,inend);
return root;
}
//根据二叉树创建字符串
public String tree2str(TreeNode root){
StringBuilder stringBuilder = new StringBuilder();
tree2strChild(root,stringBuilder);
return stringBuilder.toString();
}
public void tree2strChild(TreeNode t,StringBuilder stringBuilder){
if (t == null){
return ;
}
stringBuilder.append(t.val);
if (t.left != null){
stringBuilder.append("(");
tree2strChild(t.left,stringBuilder);
stringBuilder.append(")");
}else {
if (t.right == null){
return ;
}else {
stringBuilder.append("()");
}
}
//判断右树
if (t.right != null){
stringBuilder.append("(");
tree2strChild(t.right,stringBuilder);
stringBuilder.append(")");
}else {
return;
}
}
//二叉树前序非递归遍历实现
void preOrderNor(TreeNode root) {
if (root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
System.out.print(cur.val + " ");
cur = cur.left;
}
TreeNode top = stack.pop();
cur = top.right;
}
}
//二叉树中序非递归遍历实现
void inOrderNor(TreeNode root) {
if (root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.pop();
System.out.print(top.val + " ");
cur = top.right;
}
}