前面我们了解了 二叉树的基本概念,以及二叉树的三种遍历方式,今天我们来学习二叉树的基本操作
实现方法:
size
方法:
/**
* 获取树种节点的个数
* 子问题
* @param root
* @return
*/
public int size(TreeNode root) {
// 空树
if (root == null) {
return 0;
}
// 左子树的个数+右子树的个数+1
int nums = size(root.left) + size(root.right) + 1;
return nums;
}
测试代码:
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
BinaryTree.TreeNode root = binaryTree.createTree(); // 创建二叉树
int num = binaryTree.size(root); // 调用size方法
System.out.println("二叉树的节点个数:" + num);
}
}
实现方法:
getLeafNodeCount
方法:
/**
* 获取叶子节点的个数
* @param root
* @return
*/
public int getLeafNodeCount(TreeNode root) {
// 空树
if (root == null) {
return 0;
}
// 只有头节点
if (root.left == null && root.right == null) {
return 1;
}
int i = getLeafNodeCount(root.left); // 左子树的个数
int j = getLeafNodeCount(root.right); // 右子树的个数
return (i + j);
}
测试代码:
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
BinaryTree.TreeNode root = binaryTree.createTree(); // 创建二叉树
int num = binaryTree.getLeafNodeCount(root); //
System.out.println("叶子节点的个数" + num);
}
}
实现方法:
k-1
层之和getKLevelNodeCount
方法:
/**
* 获取第k层节点的个数
* @param root 树的头节点
* @param k 第k层
* @return
*/
public int getKLevelNodeCount(TreeNode root, int k) {
if (root == null) {
return 0;
}
if (k == 1) {
return 1;
}
int i = getKLevelNodeCount(root.left, k - 1);
int j = getKLevelNodeCount(root.right, k - 1);
return i + j;
}
测试代码:
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
BinaryTree.TreeNode root = binaryTree.createTree(); // 创建二叉树
int k = 2;
int num = binaryTree.getKLevelNodeCount(root, k);
System.out.println("第" + k +"层的节点个数:" + num);
}
}
实现方法:
getHeight
方法:
/**
* 获取二叉树的高度
* @param root
* @return
*/
public int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int i = getHeight(root.left); // 左子树的高度
int j = getHeight(root.right); // 右子树的高度
return Math.max(i, j) + 1;
}
测试代码:
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
BinaryTree.TreeNode root = binaryTree.createTree(); // 创建二叉树
int height = binaryTree.getHeight(root);
System.out.println("二叉树的高度:" + height);
}
}
实现方法:
null
;null
find
方法:
/**
* 判断val是否存在
* @param root 头节点
* @param val 要查找的值
* @return
*/
public TreeNode find(TreeNode root, int val) {
if (root == null) {
return null;
}
// 判断根节点是否是要找的数据
if (root.val == val) {
return root;
}
TreeNode leftVal = find(root.left, val);
if (leftVal != null) {
return leftVal;
}
TreeNode rightVal = find(root.right, val);
if (rightVal != null) {
return rightVal;
}
return null;
}
测试代码:
public class Test {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
BinaryTree.TreeNode root = binaryTree.createTree();
BinaryTree.TreeNode num = binaryTree.find(root, 'A');
if (num == null) {
System.out.println("没有找到要查找的值");
return;
}
System.out.println(num.val);
}
}
public class BinaryTree {
// 节点类
static class TreeNode{
public char val;
public TreeNode left; // 左节点
public TreeNode right; // 右节点
// 构造方法
public TreeNode(char val) {
this.val = val;
}
}
// 以穷举的方式 创建一颗二叉树
public TreeNode createTree() {
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
TreeNode E = new TreeNode('E');
TreeNode F = new TreeNode('F');
TreeNode G = new TreeNode('G');
TreeNode H = new TreeNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
E.right = H;
C.left = F;
C.right = G;
return A; // 返回根节点
}
// 前序遍历
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 + " ");
}
/**
* 获取树种节点的个数
* 子问题
* @param root
* @return
*/
public int size(TreeNode root) {
// 空树
if (root == null) {
return 0;
}
// 左子树的个数+右子树的个数+1
int nums = size(root.left) + size(root.right) + 1;
return nums;
}
/**
* 获取叶子节点的个数
* @param root
* @return
*/
public int getLeafNodeCount(TreeNode root) {
// 空树
if (root == null) {
return 0;
}
// 只有头节点
if (root.left == null && root.right == null) {
return 1;
}
int i = getLeafNodeCount(root.left); // 左子树的个数
int j = getLeafNodeCount(root.right); // 右子树的个数
return (i + j);
}
/**
* 获取第k层节点的个数
* @param root 树的头节点
* @param k 第k层
* @return
*/
public int getKLevelNodeCount(TreeNode root, int k) {
if (root == null) {
return 0;
}
if (k == 1) {
return 1;
}
int i = getKLevelNodeCount(root.left, k - 1);
int j = getKLevelNodeCount(root.right, k - 1);
return i + j;
}
/**
* 获取二叉树的高度
* @param root
* @return
*/
public int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int i = getHeight(root.left); // 左子树的高度
int j = getHeight(root.right); // 右子树的高度
return Math.max(i, j) + 1;
}
/**
* 判断val是否存在
* @param root 头节点
* @param val 要查找的值
* @return
*/
public TreeNode find(TreeNode root, int val) {
if (root == null) {
return null;
}
// 判断根节点是否是要找的数据
if (root.val == val) {
return root;
}
TreeNode leftVal = find(root.left, val);
if (leftVal != null) {
return leftVal;
}
TreeNode rightVal = find(root.right, val);
if (rightVal != null) {
return rightVal;
}
return null;
}
}
奋笔疾书中…
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有