Package adt.tree

Examples of adt.tree.TreeNode


import adt.tree.TreeNode;

public class Q064_Check_if_all_leaves_are_at_same_level {

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


import adt.tree.TreeNode;

public class Q032_Check_if_a_given_Binary_Tree_is_SumTree {

  public static void main(String[] args) {
    TreeNode root = Tree.sumtree();
    boolean res = isSumTree(root);
    System.out.println(res);
  }
View Full Code Here

public class Q074_Construct_a_tree_from_Inorder_and_Level_order_traversals {

  public static void main(String[] args) {
    int[] in = {4, 8, 10, 12, 14, 20, 22, 25};
    int[] level = {20, 8, 22, 4, 12, 25, 10, 14};
    TreeNode root = constructTree(in, level, 8);
    root.print();
  }
View Full Code Here

      return null;
    }
   
    // root node
    int val = level[0];
    TreeNode root = new TreeNode(level[0]);
   
    int index = map.get(val);
    int n_left = index - i;
    int n_right = n - n_left - 1;
   
View Full Code Here

import adt.tree.TreeNode;

public class Q018_Decide_if_tree2_is_a_subtree_of_tree1 {

  public static void main(String[] args) {
    TreeNode t1 = Tree.tree3();
    TreeNode t2 = Tree.tree4();
    boolean res = containsTree(t1, t2);
    System.out.println(res);
  }
View Full Code Here

import adt.tree.TreeNode;

public class Q070_Sum_of_all_the_numbers_that_are_formed_from_root_to_leaf_paths {

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

import adt.tree.TreeNode;

public class Q024_Print_nodes_at_k_distance_from_root {

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

import adt.tree.TreeNode;

public class Q072_Find_distance_between_two_given_keys_of_a_Binary_Tree {

  public static void main(String[] args) {
    TreeNode root = Tree.tree15();
    int res = dist(root, 5, 8);
    System.out.println(res);
  }
View Full Code Here

    System.out.println(res);
  }
 
  static int dist(TreeNode root, int v1, int v2) {
    // step 1. get the LCA
    TreeNode lca = LCA(root, v1, v2);
   
    if (lca == null) {
      return -1;
    }
   
View Full Code Here

    if (root.val == v1 || root.val == v2) {
      return root;
    }
   
    // 从左右子树去找他们的最小公共父节点
    TreeNode left = LCA(root.left, v1, v2);
    TreeNode right = LCA(root.right, v1, v2);
   
    // 如果v1, v2分别在两颗子树上,那么root就是最小公共父节点
    if (left != null && right != null) {
      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.