赞
踩
从百科摘个定义:
在计算机科学中,二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。
一棵深度为k,且有2^k-1个结点的二叉树,称为满二叉树。这种树的特点是每一层上的结点数都是最大结点数。而在一棵二叉树中,除最后一层外,若其余层都是满的,并且或者最后一层是满的,或者是在右边缺少连续若干结点,则此二叉树为完全二叉树。具有n个结点的完全二叉树的深度为floor(log2n)+1。深度为k的完全二叉树,至少有2k-1个叶子结点,至多有2k-1个结点。
- public class TreeNode{
- int data; //节点内的元素
- TreeNode leftNode; //左子树
- TreeNode rightNode; //右子树
- public TreeNode(int data,TreeNode leftNode,TreeNode rightNode){
- this.data =data;
- this.leftNode = leftNode;
- this.rightNode = rightNode;
- }
-
- public String toString() {
- return "TreeNode [data=" + data + ", leftNode=" + leftNode + ", rightNode=" + rightNode + "]";
- }
-
-
- }
图片来自:https://blog.csdn.net/qq_36186690/article/details/80699770
主要实现依赖了递归。初始化跟求叶子节点和都是。
- /**
- *
- * @author bohu83
- *
- */
- public class LeftsubTreeSum {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int num[] = {4,2,1,3,5,7,6,8};
- //init
- TreeNode node = new TreeNode(num[0],null,null);
- for(int i=1;i< num.length;i++){
- add(node,new TreeNode(num[i],null,null) );
- }
- System.out.println(node);
- //leftleaf求和
- int lsum =leftLeafSum(node);
- System.out.println(lsum);
- //rightleaf求和
- int rsum =rightLeafSum(node);
- System.out.println(rsum);
- }
-
- public static int rightLeafSum( TreeNode root ){
- int rres =0;
- if(root == null){
- return 0;
- }
- if(root.rightNode!=null && root.rightNode.leftNode== null && root.rightNode.rightNode== null ){
- rres += root.rightNode.data;
- }
- return rres+ rightLeafSum(root.leftNode )+ rightLeafSum( root.rightNode);
-
- }
-
-
- public static int leftLeafSum(TreeNode root ){
-
- int res = 0;
- if(root == null) {
- return res;
- }
- //判断叶子节点
- if(root.leftNode != null && root.leftNode.leftNode== null && root.leftNode.rightNode== null){
- res += root.leftNode.data;
- }
- return res+ leftLeafSum(root.leftNode )+leftLeafSum(root.rightNode );
-
- }
-
- public static void add(TreeNode node,TreeNode next){
- //插右侧
- if(next.data >= node.data){
- if(node.rightNode== null){
- node.rightNode = next;
- }else{//递归
- add(node.rightNode,next);
- }
- }else{
- //插左侧
- if(node.leftNode == null){
- node.leftNode = next;
- }else{
- add(node.leftNode,next);
- }
- }
- }
-
- static class TreeNode{
- int data; //节点内的元素
- TreeNode leftNode; //左子树
- TreeNode rightNode; //右子树
- public TreeNode(int data,TreeNode leftNode,TreeNode rightNode){
- this.data =data;
- this.leftNode = leftNode;
- this.rightNode = rightNode;
- }
-
- public String toString() {
- return "TreeNode [data=" + data + ", leftNode=" + leftNode + ", rightNode=" + rightNode + "]";
- }
- }
- }
输出:
TreeNode [data=4, leftNode=TreeNode [data=2, leftNode=TreeNode [data=1, leftNode=null, rightNode=null], rightNode=TreeNode [data=3, leftNode=null, rightNode=null]], rightNode=TreeNode [data=5, leftNode=null, rightNode=TreeNode [data=7, leftNode=TreeNode [data=6, leftNode=null, rightNode=null], rightNode=TreeNode [data=8, leftNode=null, rightNode=null]]]]
7
11
参考:
https://blog.csdn.net/qq_36186690/article/details/80699770
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。