zoukankan      html  css  js  c++  java
  • 二叉树的定义

    /** class for nodes used in a binary tree */
    
    package dataStructures;
    
    public class BinaryTreeNode {
        // package visible data members
        Object element;
        BinaryTreeNode leftChild; // left subtree
        BinaryTreeNode rightChild; // right subtree
    
        // constructors
        public BinaryTreeNode() {
        }
    
        public BinaryTreeNode(Object theElement) {
            element = theElement;
        }
    
        public BinaryTreeNode(Object theElement, BinaryTreeNode theleftChild,
                BinaryTreeNode therightChild) {
            element = theElement;
            leftChild = theleftChild;
            rightChild = therightChild;
        }
    
        // accessor methods
        public BinaryTreeNode getLeftChild() {
            return leftChild;
        }
    
        public BinaryTreeNode getRightChild() {
            return rightChild;
        }
    
        public Object getElement() {
            return element;
        }
    
        // mutator methods 设值方法
        public void setLeftChild(BinaryTreeNode theLeftChild) {
            leftChild = theLeftChild;
        }
    
        public void setRightChild(BinaryTreeNode theRightChild) {
            rightChild = theRightChild;
        }
    
        public void setElement(Object theElement) {
            element = theElement;
        }
    
        // output method
        public String toString() {
            return element.toString();
        }
        
    }

     求二叉树的节点数最多的层

    public static int maxLevel(BinaryNode<Integer> t){
            if(t==null){
                return 0;
            }
            BinaryNode<Integer> endOfLevel = new BinaryNode<Integer>();
            ArrayDeque<BinaryNode<Integer>> p = new ArrayDeque<>();
            p.add(t);
            p.add(endOfLevel);
            int numOfNodes = 0;
            int maxLevel = 0;
            int maxNum = 0;
            int currentLevel = 1;
            while(true){
                BinaryNode<Integer> temp = p.poll();
                if(temp.equals(endOfLevel)){
                    if(numOfNodes>maxNum){
                        maxNum = numOfNodes;
                        maxLevel = currentLevel;
                    }else if(numOfNodes==0){
                        return maxLevel;
                    }
                    currentLevel++;
                    numOfNodes = 0;
                    p.add(endOfLevel);
                }else{
                    numOfNodes++;
                    if(temp.left!=null){
                        p.add(temp.left);
                    }
                    if(temp.right!=null){
                        p.add(temp.right);
                    }
                }
            }
        }
  • 相关阅读:
    Oracle学习(一)SQL基础
    结构型设计模式(二)桥接模式
    dubbo学习(十)spring boot整合dubbo
    dubbo学习(九)dubbo监控中心
    结构型设计模式(一)适配器模式
    取石子游戏
    卡特兰数
    做题中踩过的坑。。。
    51Nod1130斯特林近似
    51Nod1089最长回文子串 V2(Manacher算法)
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/4225480.html
Copyright © 2011-2022 走看看