Package adt.tree

Examples of adt.tree.TreeNode


public class Q060_Construct_Complete_Binary_Tree_from_its_Linked_List_Representation {

  public static void main(String[] args) {
    LinkedList list = new LinkedList(new int[]{10, 12, 15, 25, 30, 36});
    TreeNode root = constructTree(list.head);
    root.print();
  }
View Full Code Here


      return null;
    }
   
    // 利用一个Queue
    Queue<TreeNode> queue = new java.util.LinkedList<TreeNode>();
    TreeNode root = new TreeNode(head.val);
    queue.add(root);
   
    head = head.next;
   
    while (head != null) {
      TreeNode node = queue.poll();
     
      // left
      TreeNode left = new TreeNode(head.val);
      queue.add(left);
      head = head.next;
     
      // right
      TreeNode right = null;
      if (head != null) {
        right = new TreeNode(head.val);
        queue.add(right);
        head = head.next;
      }
     
      node.left = left;
View Full Code Here

import adt.tree.TreeNode;

public class Q075_Reverse_alternate_levels_of_a_perfect_binary_tree {

  public static void main(String[] args) {
    TreeNode root = Tree.tree16();
    reverseAlternateLevels(root);
    root.print();
  }
View Full Code Here

   
    // --------------------------------
    //  1st level order: store values
    // --------------------------------
    while (!curr.isEmpty()) {
      TreeNode node = curr.pollLast();
      list.add(node.val);
     
      if (level % 2 == 0) {
        if (node.left != null) {
          next.add(node.left);
        }
        if (node.right != null) {
          next.add(node.right);
        }
      } else {
        if (node.right != null) {
          next.add(node.right);
        }
        if (node.left != null) {
          next.add(node.left);
        }
      }
     
      if (curr.isEmpty()) {
        curr = next;
        next = new ArrayDeque<TreeNode>();
        level++;
      }
    }
   
    // --------------------------------
    //  2nd level order: update values
    // --------------------------------
    curr = new ArrayDeque<TreeNode>();
    next = new ArrayDeque<TreeNode>();
   
    curr.add(root);
   
    int index = 0;
   
    while (!curr.isEmpty()) {
      TreeNode node = curr.pollFirst();
      // update the value
      node.val = list.get(index++);
     
      if (node.left != null) {
        next.add(node.left);
View Full Code Here

public class Q026A_Sorted_Linked_List_to_Balanced_BST {

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

      return null;
    }
   
    // 务必单独考虑只剩下一个节点的时候, 否则会死循环!
    if (head.next == null) {
      return new TreeNode(head.val);
    }
   
    // step 1. find the middle node
    ListNode prev = null;
    ListNode slow = head;
    ListNode fast = head;
   
    while (fast != null && fast.next != null) {
      prev = slow;
      slow = slow.next;
      fast = fast.next.next;
    }
   
    // step 2. create the root node
    TreeNode root = new TreeNode(slow.val);
   
    // step 3. build left and right trees recursively
    // 先把前面的链表切断
    prev.next = null;
   
View Full Code Here

import adt.tree.TreeNode;

public class Q078_Check_if_two_nodes_are_cousins_in_a_Binary_Tree {

  public static void main(String[] args) {
    TreeNode root = Tree.bst2();
    TreeNode a = Tree.getTreeNode(root, 4);
    TreeNode b = Tree.getTreeNode(root, 25);
   
    boolean res = areCousins(root, a, b);
    System.out.println(res);
  }
View Full Code Here

public class Q042_Find_the_maximum_sum_leaf_to_root_path_in_a_Binary_Tree {

  static int max;
 
  public static void main(String[] args) {
    TreeNode root = Tree.bst2();
    max = 0;
    maxSumPath(root, 0);
   
    System.out.println(max);
  }
View Full Code Here

import adt.tree.TreeNode;

public class Q029_Print_Ancestors_of_a_given_node_in_Binary_Tree {

  public static void main(String[] args) {
    TreeNode root = Tree.bst2();
    printAncestorsR(root, 10);
  }
View Full Code Here

import adt.tree.TreeNode;

public class Q051_Convert_a_BST_to_a_Binary_Tree_such_that_sum_of_all_greater_keys_is_added_to_every_key {

  public static void main(String[] args) {
    TreeNode root = Tree.bst1();
    convertBST(root);
    root.print();
  }
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.