package com.java.aiwork.util.tree; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * @author wsjcz * @Date 2019/09/17 * */ public class BTree { private int data; private BTree left; private BTree right; public BTree(int data) { this.data=data; left=null; right=null; } public int getData() { return data; } //可以自定义需求创建 public void insert(BTree root, int data) { // 向插入子节点 if (data > root.data) // 二叉树的左节点都比根节点小 { if (root.right == null) { root.right = new BTree(data); } else { this.insert(root.right, data); } } else { // 二叉树的右节点都比根节点大 if (root.left == null) { root.left = new BTree(data); } else { this.insert(root.left, data); } } } private static int
java 二叉树求和
最新推荐文章于 2024-04-18 14:10:46 发布