二叉树(java):【1】求二叉树的深度,【2】非递归中序遍历二叉树,【3】递归算法复制二叉树T为一棵新的二叉树,【4】非递归算法计算T中的叶子数,【5】用递归算法计算T中度为1的结点数

目录

【1】求二叉树的深度

【2】非递归中序遍历二叉树

【3】递归算法复制二叉树T为一棵新的二叉树

【4】非递归算法计算T中的叶子数

【5】用递归算法计算T中度为1的结点数

【6】非递归后序遍历二叉树


【1】求二叉树的深度

    //求二叉树的深度
	private static int high(btnode T) {
		if(T==null)
			return 0;
		else 
			return Math.max(high(T.left), high(T.right))+1;
	}

【2】非递归中序遍历二叉树

    //非递归算法实现中序遍历
    private static void nonRecInorder(btnode T) {
		String s = "";
		btnode temp = T;
		Stack<btnode> stack = new Stack<>();
		while(true) {
			while(temp != null) {
				stack.push(temp);//入栈
				temp = temp.left;
			}
			//当栈为空的时候,就表示二叉树遍历结束
			if(stack.isEmpty()) {
				break;
			}
			temp = stack.pop(); //出栈
			s+=temp.ele+"";
			temp = temp.right;
		}
		System.out.println("中序遍历:"+s);
	}

【3】递归算法复制二叉树T为一棵新的二叉树

//复制二叉树T为一棵新的二叉树,返回新二叉树根节点的指针
    private static btnode copyBtree(btnode T, btnode t) {
    	if(T==null)
    		return null;
    	t.setEle(T.ele);
    	t.setLeft(T.left);
    	t.setRight(T.right);
    	copyBtree(T.left,t.left);
    	copyBtree(T.right,t.right);
    	return t;
	}

【4】非递归算法计算T中的叶子数

//	题目4 :编写程序,用非递归算法计算二叉树T有多少片叶子。
	private static int nonRecCountLeaf(btnode T) {
		String s = "";
		btnode temp = T;
		Stack<btnode> stack = new Stack<>();
		int leaves = 0;
		//非递归中序遍历
		while(true) {
			while(temp != null) {
				stack.push(temp);//入栈
				temp = temp.left;
			}
			
			/*如果当前结点是空,则leaves++,
			但当前结点是叶子的左右结点,
			也就是每个叶子结点对应的两个空结点,
			导致计算到的leaves为叶子结点的两倍*/
			leaves++;
			
			//当栈为空的时候,就表示二叉树遍历结束
			if(stack.isEmpty()) {
				break;
			}
			temp = stack.pop(); //出栈
			temp = temp.right;
		}
		//记得除于2
		return leaves/2;
	}

【5】用递归算法计算T中度为1的结点数

//	用递归算法计算二叉树T中度为1的结点数。
    private static int countSingleC(btnode T) {
    	//如果当前结点是空结点,就如return 0
    	if(T==null) {
    		return 0;
    	}
    	//如果当前结点只有一个结点,就+1
    	if((T.left!=null&&T.right==null)||(T.left==null&&T.right!=null))
    		return 1 + countSingleC(T.left) + countSingleC(T.right);
    	//如果当前结点有0个或2个结点,就继续左右子数遍历
    	return countSingleC(T.left) + countSingleC(T.right);
	}

【6】非递归后序遍历二叉树

//非递归算法实现后序遍历
	   public static void nonRecSucorder(btnode T) {
	        Stack<btnode> stack = new Stack();
	        // 当前结点
	        btnode now = T;
	        // 刚被弹出栈的结点(表示遍历过的结点)
	        btnode pre = null;
	        System.out.print("后序遍历:");
	        while (!stack.isEmpty() || now != null) {
	            // 当 cur 不为空时, 将左边的结点一直压栈
	            while (now != null) {
	                stack.push(now);
	                now = now.left;
	            }
	            // 当 cur 为空时, 查看栈顶元素
	            btnode top = stack.peek();
	            
	            // 查看栈顶元素是否有右节点
	            // 1. 当无右节点时, 输出该节点的值, 并将该节点从栈中弹出, 此时需要记录刚被弹出的结点
	            // 2. 当有右节点并且右节点没有被遍历过, 此时就将当前节点指向其右节点.
	            // 3. 当有右节点时, 并且右节点已经被遍历过, 此时将该节点弹出, 记录弹出的结点.
	            if (top.right == null) { // 无右孩子
	                System.out.print(top.ele);
	                pre = stack.pop();
	            } 
	            // 有右孩子并且右孩子已经被遍历过
	            else if (top .right == pre) {
	                System.out.print(top.ele);
	                pre = stack.pop();
	            }
	            // 右孩子没有被遍历过.
	            else {
	            	now = top.right;
	            }
	        }
	        System.out.println();
	    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值