【2019春招准备:常见算法题1-10】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33907408/article/details/84932909

【内容】

  1. topK
  2. 台阶
  3. 非递归遍历二叉树
    【补充】

==================

1.topK

2.青蛙跳台阶

【@深信服 大数据开发】

【题目描述 】
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

【解1】
根据上述测试用例及结果,差不多已经可以看出来一些规律了。
但是这里再进行一细节方面的分析:
f(1) = 1
f(2) = f(2-1) + f(2-2) //f(2-2) 表示2阶一次跳2阶的次数。
f(3) = f(3-1) + f(3-2) + f(3-3)

f(n) = f(n-1) + f(n-2) + f(n-3) + … + f(n-(n-1)) + f(n-n)
由上述可推导出:
f(n-1) = f(0) + f(1) + f(2) + f(3) + … + f(n-2)
f(n) = f(0) + f(1) + f(2) + f(3) + … + f(n-2) + f(n-1) = f(n-1) + f(n-1)
则可以得出最终结论,在n阶台阶,一次有1、2、…n阶的跳的方式时,总得跳法为:
f(n) = 0 ,(n=0 )
f(n) = 1 ,(n=1 )
f(n) = 2*f(n-1),(n>=2)
以上,可以看出,若台阶大于0的话,青蛙跳的方法共有2(n-1)种。

【解2】
鸟瞰整个阶梯,除开最后一级台阶(n),每一个台阶都有跳或者不跳的可能。

3. 非递归遍历二叉树

package q2_tree;

import java.util.LinkedList;
import java.util.Random;

/**
 * 二叉树的非递归遍历 linkedList的push(addFirst)是加在头部,pop也是弄出头部元素
 * 
 * @author ziboris
 * @date 2018年12月22日 下午2:31:31
 *
 */
public class Test3_noRecurseTree {
	// 自己是一个BinaryTree
	// 随机生成树:左节点还是右节点
	private static final Random rand = new Random();

	/*
	 * 叶子节点
	 */
	private static class BinaryNode {
		int ele;
		BinaryNode left;
		BinaryNode right;
		boolean isFirst;// 专门给后序非递归遍历判定的时候用,判定是否已经访问过

		public BinaryNode(int ele) {
			this.ele = ele;
			this.left = this.right = null;
			this.isFirst = false;
		}
	}

	private BinaryNode root;// 根节点

	/*
	 * 固定元素随机形状建树
	 */
	private void buildTree() {
		int[] nodes = { 3, 0, 7, 4, 9, 10, 45 };
		for (int i : nodes) {
			insert(i);
		}
	}

	private BinaryNode insert(int ele) {
		return root = insert(root, ele);
	}

	private BinaryNode insert(BinaryNode root, int ele) {
		if (null == root) {
			return root = new BinaryNode(ele);
		}
		if (rand.nextInt() % 2 == 0)
			root.left = insert(root.left, ele);
		else
			root.right = insert(root.right, ele);
		return root;
	}

	// #=====================================================================
	// #====================== pre ======================
	// #=====================================================================
	public void preOrder() {
		preorder(root);
	}

	private void preorder(BinaryNode root) {
		if (root == null) {
			return;
		}
		System.out.print(root.ele + " ");
		preorder(root.left);
		preorder(root.right);
	}

	public void nonRecurPreTraverse() {
		if (null == root)
			return;
		nonRecurPreTraverse(root);
	}

	private void nonRecurPreTraverse(BinaryNode root) {
		LinkedList<BinaryNode> stack = new LinkedList<Test3_noRecurseTree.BinaryNode>();
		BinaryNode currentNode, tmp;
		currentNode = root;
		while (currentNode != null || !stack.isEmpty()) {// stack空了或者当前节点为空结束遍历
			while (currentNode != null) {
				System.out.print(currentNode.ele + " ");
				stack.push(currentNode);
				currentNode = currentNode.left;
			}

			if (!stack.isEmpty()) {
				tmp = stack.pop();
				currentNode = tmp.right;
			}
		}
	}

	// #=====================================================================
	// #====================== in ======================
	// #=====================================================================
	public void inOrder() {
		inorder(root);
	}

	private void inorder(BinaryNode root) {
		if (root == null) {
			return;
		}
		inorder(root.left);
		System.out.print(root.ele + " ");
		inorder(root.right);
	}

	public void nonRecurInTraverse() {
		if (null == root)
			return;
		nonRecurInTraverse(root);
	}

	private void nonRecurInTraverse(BinaryNode root) {
		LinkedList<BinaryNode> stack = new LinkedList<Test3_noRecurseTree.BinaryNode>();
		BinaryNode currentNode, tmp;
		currentNode = root;

		while (currentNode != null || !stack.isEmpty()) {
			while (currentNode != null) {
				stack.push(currentNode);
				currentNode = currentNode.left;
			}
			if (!stack.isEmpty()) {
				// 理解成将越左下角的节点放在stack的顶端,首先pop出来展示,然后是root节点,最后剩下右边的节点
				tmp = stack.pop();
				System.out.print(tmp.ele + " ");
				currentNode = tmp.right;
			}
		}
	}

	// #=====================================================================
	// #====================== post ======================
	// #=====================================================================
	public void postOrder() {
		postorder(root);
	}

	private void postorder(BinaryNode root) {
		if (root == null) {
			return;
		}
		postorder(root.left);
		postorder(root.right);
		System.out.print(root.ele + " ");
	}

	public void nonRecurPostTraverse() {
		if (root == null)
			return;
		nonRecurPostTraverse(root);
	}

	private void nonRecurPostTraverse(BinaryNode root) {
		LinkedList<BinaryNode> stack = new LinkedList<Test3_noRecurseTree.BinaryNode>();
		BinaryNode currentNode, tmp;
		currentNode = root;
		while (currentNode != null && !stack.isEmpty()) {
			while (currentNode != null) {
				stack.push(currentNode);
				currentNode = currentNode.left;
			}
			if (!stack.isEmpty()) {
				tmp = stack.getFirst();
				if (tmp.isFirst == false) {
					tmp.isFirst = true;
					currentNode = tmp.right;
				} else {
					tmp = stack.pop();
					System.out.print(tmp.ele + " ");
				}
			}
		}
	}

	/*
	 * MainTest=-=================================================================
	 */

	public static void main(String[] args) {
		Test3_noRecurseTree mbt = new Test3_noRecurseTree();
		mbt.buildTree();

		System.out.println("========pre order:=========");
		mbt.preOrder();
		System.out.println();
		mbt.nonRecurPreTraverse();

		System.out.println("\n========in order:=========");
		mbt.inOrder();
		System.out.println();
		mbt.nonRecurInTraverse();

		System.out.println("\n========post order:=========");
		mbt.postOrder();
		System.out.println();
		mbt.nonRecurPostTraverse();
	}

}

猜你喜欢

转载自blog.csdn.net/qq_33907408/article/details/84932909