Posts

Showing posts with the label open dsa

7.7.12. Binary Tree Has Path Sum Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-has-path-sum-exercise X282: Binary Tree Has Path Sum Exercise We define a "root-to-leaf path" to be any sequence of nodes in a tree starting with the root node and proceeding downward to a leaf. The "root-to-leaf path sum" for that path is the sum of the values for all the nodes (including the root) along that path. Define an empty tree to contain no root-to-leaf paths (and so its sum is zero). Define a tree with one node to have a root-to-leaf path consisting of just the root (and so its sum is the value of the root). Given a binary tree and a value  sum , return true if the tree has some root-to-leaf path such that adding up all the values along the path equals  sum . Return false if no such path exists. Here are methods that you can use on the  BinNode  objects:    interface BinNode {       public int value(); ...

7.7.11. Binary Tree Get Difference Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-get-difference-exercise X290: Binary Tree Get Difference Exercise Given a binary tree, write a recursive function to return the difference between the sum of all node values at odd levels and sum of all node values at even levels. Define the root node to be at level 1. Here are methods that you can use on the  BinNode  objects:    interface BinNode {       public int value();       public void setValue(int v);       public BinNode left();       public BinNode right();       public boolean isLeaf();    }

7.7.10. Binary Tree Height Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-height-exercise X285: Binary Tree Height Exercise The height of a binary tree is the length of the path to the deepest node. An empty tree has a height of 0, a tree with one node has a height of 1, and so on. Write a recursive function to find the height of the binary tree pointed at by  root . Here are methods that you can use on the  BinNode  objects:    interface BinNode {       public int value();       public void setValue(int v);       public BinNode left();       public BinNode right();       public boolean isLeaf();    } ----

7.7.8. Binary Tree Check Value Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-check-value-exercise X280: Binary Tree Check Value Exercise Write a recursive function that returns true if there is a node in the given binary tree with the given value, and false otherwise. Note that this tree is  not  a Binary Search Tree. Here are methods that you can use on the  BinNode  objects: 1 interface BinNode { 2 public int value (); 3 public void setValue ( int v ); 4 public BinNode left (); 5 public BinNode right (); 6 public boolean isLeaf (); 7 } ---

7.7.6. Binary Tree Sum Nodes Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-sum-nodes-exercise X283: Binary Tree Sum Nodes Exercise Write a recursive function  int BTsumall(BinNode root)  that returns the sum of the values for all of the nodes of the binary tree with root  root . Here are methods that you can use on the  BinNode  objects: 1 interface BinNode { 2 public int value (); 3 public void setValue ( int v ); 4 public BinNode left (); 5 public BinNode right (); 6 public boolean isLeaf (); 7 } ----

7.7.4. Binary Tree Check Sum Exercise

https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-check-sum-exercise X286: Binary Tree Check Sum Exercise Given a binary tree, check if the tree satisfies the property that for each node, the sum of the values of its left and right children are equal to the node's value. If a node has only one child, then the node should have the same value as that child. Leaf nodes automatically satisfy the property. Here are methods that you can use on the  BinNode  objects:    interface BinNode {       public int value();       public void setValue(int v);       public BinNode left();       public BinNode right();       public boolean isLeaf();    } ---

7.7.5. Binary Tree Leaf Nodes Count Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-leaf-nodes-count-exercise X287: Binary Tree Leaf Nodes Count Exercise Write a recursive function  int BTleaf(BinNode root)  to count the number of leaf nodes in the binary tree pointed at by  root . You must use the  isLeaf()  method in the  BinNode  class to check if a node is a leaf. This is the definition of the  BinNode  class: 1 interface BinNode { 2 public int value (); 3 public void setValue ( int v ); 4 public BinNode left (); 5 public BinNode right (); 6 public boolean isLeaf (); 7 } Function   Base condition   If null     return 0   If leaf     return 1  return left subtree + right subtree Each function just does its job, and sends subtree to recursive functions

7.7.2. Binary Tree Set Depth Exercise

Source https://opendsa-server.cs.vt.edu/ODSA/Books/CS3/html/BinaryTreeInfFlw.html#binary-tree-set-depth-exercise X281: Binary Tree Set Depth Exercise Write a recursive function to set the value for each node in a binary tree to be its depth then return the modified tree. Assume that nodes store integer values. On the initial call to  BTsetdepth ,  depth  is 0. Here are methods that you can use on the  BinNode  objects:    interface BinNode {       public int value();       public void setValue(int v);       public BinNode left();       public BinNode right();       public boolean isLeaf();    }