- //递归中序遍历
- public void inorder() {
- System.out.print("binaryTree递归中序遍历:");
- inorderTraverseRecursion(root);
- System.out.println();
- }
- //层次遍历
- public void layerorder() {
- System.out.print("binaryTree层次遍历:");
- LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>();
- queue.addLast(root);
- Node<Integer> current = null;
- while(!queue.isEmpty()) {
- current = queue.removeFirst();
- if (current.getLeftChild() != null)
- queue.addLast(current.getLeftChild());
- if (current.getRightChild() != null)
- queue.addLast(current.getRightChild());
- System.out.print(current.getValue());
- }
- System.out.println();
- }
- //获得二叉树深度
- public int getDepth() {
- return getDepthRecursion(root);
- }
- private int getDepthRecursion(Node<Integer> node){
- if (node == null)
- return 0;
- int llen = getDepthRecursion(node.getLeftChild());
- int rlen = getDepthRecursion(node.getRightChild());
- int maxlen = Math.max(llen, rlen);
- return maxlen + 1;
- }
- //递归先序遍历
- public void preorder() {
- System.out.print("binaryTree递归先序遍历:");
- preorderTraverseRecursion(root);
- System.out.println();
- }
时间: 2024-10-27 18:43:55