欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

BST Traversal

程序员文章站 2022-04-24 23:47:15
...

Prompt

Write three functions that take in a Binary Search Tree (BST) and an empty array, traverse the BST, add its nodes’ values to the input array, and return that array. The three functions should traverse the BST using the
in-order, pre-order, and post-order treetraversal techniques, respectively. If you’re unfamiliar with tree-traversal
techniques, we recommend watching the Conceptual Overview section of this question’s video explanation before starting to code.

Each BST node has an integer value , a left child node, and a right child node. A node is said to be a valid BST node if and only if it satises the BST property: its value is strictly greater than the values of every node to its left; its value is less than or equal to the values of every node to its right; and its children nodes are either valid
BST nodes themselves or None / null .

Sample Input

BST Traversal

Sample Output

inOrderTraverse: [1, 2, 5, 5, 10, 15, 22]
preOrderTraverse: [10, 5, 2, 1, 5, 15, 22]
postOrderTraverse: [1, 2, 5, 5, 22, 15, 10]

Solution

import java.util.List;

class Program {
  public static List<Integer> inOrderTraverse(BST tree, List<Integer> array) {
		// O(n) time | O(h) 
		if (tree.left != null) {
			inOrderTraverse(tree.left, array);
		}
		array.add(tree.value);
		if (tree.right != null) {
			inOrderTraverse(tree.right, array);
		}
    return array;
  }

  public static List<Integer> preOrderTraverse(BST tree, List<Integer> array) {
		array.add(tree.value);
		if (tree.left != null) {
			preOrderTraverse(tree.left, array);
		}
		if (tree.right != null) {
			preOrderTraverse(tree.right, array);
		}
    return array;
  }

  public static List<Integer> postOrderTraverse(BST tree, List<Integer> array) {
		if (tree.left != null) {
			postOrderTraverse(tree.left, array);
		}
		if (tree.right != null) {
			postOrderTraverse(tree.right, array);
		}
		array.add(tree.value);
    return array;
  }

  static class BST {
    public int value;
    public BST left;
    public BST right;

    public BST(int value) {
      this.value = value;
    }
  }
}

How to Bug Free

谨慎输出条件

  • 层次遍历
相关标签: BST