码迷,mamicode.com
首页 > 其他好文 > 详细

n级阶梯,每次走一步或两步,问最多有多少种走法 二叉树实现

时间:2018-03-30 20:01:58      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:实现   nod   static   ***   string   tree   color   set   blog   

NodeTree类

public class NodeTree {
    private int num;
    private NodeTree left;
    private NodeTree right;
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public NodeTree getLeft() {
        return left;
    }
    public void setLeft(NodeTree left) {
        this.left = left;
    }
    public NodeTree getRight() {
        return right;
    }
    public void setRight(NodeTree right) {
        this.right = right;
    }
    public NodeTree() {
        super();
    }
    public NodeTree(int num) {
        super();
        this.num = num;
    }
    public NodeTree(int num, NodeTree left, NodeTree right) {
        super();
        this.num = num;
        this.left = left;
        this.right = right;
    }
    
    

}

主要用迭代实现

public class Lianxi03 {

    private static int num=0;
    public static void main(String[] args) {
        NodeTree nodeTree=new NodeTree(0);
        
        
        method02(nodeTree,100);
        System.out.println(num);
    }
    
    

    public static void method02(NodeTree nodeTree,int num1){
        if(nodeTree.getNum()==num1){
            ++num;
        }else if (nodeTree.getNum()>num1) {
            
        }else if (nodeTree.getNum()<num1){
            int temp=nodeTree.getNum();
        //    System.out.println(temp+"******"+num);
            
            NodeTree tempnode =nodeTree;
            nodeTree.setLeft(new NodeTree());
            nodeTree=nodeTree.getLeft();
            nodeTree.setNum(temp+1);
            method02(nodeTree,num1);
            
            tempnode.setRight(new NodeTree());    
            nodeTree=tempnode.getRight();
            
            nodeTree.setNum(temp+2);
            method02(nodeTree,num1);
        }
    }
}

 

还有个例子-----数的打印

 

n级阶梯,每次走一步或两步,问最多有多少种走法 二叉树实现

标签:实现   nod   static   ***   string   tree   color   set   blog   

原文地址:https://www.cnblogs.com/developerxiaofeng/p/8677850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!