Package adt.tree

Examples of adt.tree.TreeNode


   
    return buildTree(index, listMap);
  }
 
  static TreeNode buildTree(int val, Map<Integer, ArrayList<Integer>> listMap) {
    TreeNode root = new TreeNode(val);
   
    ArrayList<Integer> list = listMap.get(val);
   
    if (list == null) {
      return root;
View Full Code Here


  static ListNode head;
 
  public static void main(String[] args) {
    LinkedList list = new LinkedList(new int[]{1, 2, 3, 4, 5, 6, 7});
    head = list.head;
    TreeNode root = sortedListToBST(7);
    root.print();
  }
View Full Code Here

import adt.tree.TreeNode;

public class Q068_Deepest_left_leaf_node_in_a_binary_tree {

  public static void main(String[] args) {
    TreeNode root = Tree.tree12();
    findDeepestLeftLeaf(root, 1, false);
    System.out.format("%d\n", val);
  }
View Full Code Here

  static TreeNode sortedListToBST(int n) {
    if (n <= 0) {
      return null;
    }
   
    TreeNode left = sortedListToBST(n/2);
   
    TreeNode root = new TreeNode(head.val);
   
    root.left = left;
   
    head = head.next;  // 这个是最巧妙的地方!
   
View Full Code Here

import adt.tree.TreeNode;

public class Q050_Floor_and_Ceil_from_a_BST {

  public static void main(String[] args) {
    TreeNode root = Tree.bst2();
   
    int ceil = findCeil(root, 17);
    System.out.format("ceil: %d\n", ceil);
   
    int floor = findFloor(root, 17);
View Full Code Here

import adt.tree.TreeNode;

public class Q003_Determine_if_Two_Trees_are_Identical {

  public static void main(String[] args) {
    TreeNode r1 = Tree.tree1();
    TreeNode r2 = Tree.bst1();
   
    boolean res = isIdentical(r1, r2);
    System.out.println(res);
  }
View Full Code Here

import adt.tree.TreeNode;

public class Q041B_Merge_two_BSTs_with_limited_extra_space {

  public static void main(String[] args) {
    TreeNode r1 = Tree.bst1();
    TreeNode r2 = Tree.bst2();
    merge(r1, r2);
  }
View Full Code Here

    }
   
    Stack<TreeNode> s1 = new Stack<TreeNode>();
    Stack<TreeNode> s2 = new Stack<TreeNode>();
   
    TreeNode curr1 = r1;
    TreeNode curr2 = r2;
   
    // Run the loop while there are nodes not yet printed.
      // The nodes may be in stack(explored, but not printed)
      // or may be not yet explored
    while (!s1.isEmpty() || !s2.isEmpty() || curr1 != null || curr2 != null) {
View Full Code Here

public class Q034_Sorted_Array_to_Balanced_BST {

  public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4};
    TreeNode tree = sortedArrayToBST(arr, 0, arr.length - 1);
    tree.print();
  }
View Full Code Here

      return null;
    }
   
    int mid = lo + (hi - lo) / 2;
   
    TreeNode root = new TreeNode(arr[mid]);
   
    root.left = sortedArrayToBST(arr, lo, mid - 1);
    root.right = sortedArrayToBST(arr, mid + 1, hi);
   
    return root;
View Full Code Here

TOP

Related Classes of adt.tree.TreeNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.