All pastes #346432 Raw Edit

Stuff

public java v1 · immutable
·published 2007-02-09 11:20 UTC
rendered paste body
1)Root - top node of a structureChild - nodes lower then another in the hierarchyLeaf - a node at the end of the treeParent -  a node higher just above the current nodeAncestor - a node somewhere above the current in the treeSub-tree - tree inside the main tree2) You need 17 levels to store 100,000 nodes.3) a) 1-min and 15 max   b) 7 min and 7 max8) It counts the number of leaves.public class exercises{  public boolean isLeaf(TreeNode node)  {    return node.getLeft() == null && node.getRight() == null;  }  public int sumTree(TreeNode root)  {    if(root == null)      return 0;    int sum = 0;    sum += (Integer)root.getValue();    sum += sumTree(root.getLeft());    sum += sumTree(root.getRight());    return sum;  }  public int countPaths(TreeNode root)  {    int sum = 0;    if(root.getLeft() != null)    {      sum++;      sum += countPaths(root.getLeft());    }    if(root.getRight() != null)    {      sum++;      sum += countPaths(root.getRight());    }    return sum;  }  public int depth(TreeNode root)  {    if(root == null)      return 0;    int d = 1;    return Math.max(d + depth(root.getLeft()),                    d + depth(root.getRight()));  }}