Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:08:41+00:00 2026-06-04T18:08:41+00:00

Skiena’s book on Algorithm contains the following question: 1) Evaluate expression given as binary

  • 0

Skiena’s book on Algorithm contains the following question:

1) Evaluate expression given as binary tree in O(n) time, given n nodes.

2) Evaluate expression given as DAG in O(n+m) time, given n nodes and m edges in DAG.

enter image description here

I could think of a way for the first question:

evaluate(node) {
     if(node has children){
          left_val = evaluate(node->left);
          right_val = evaluate(node->right);

          // find operation symbol for node and use it as
          // val = left_val operation right_val

          return val;
     }
     else {
          return node_value;
     }
}

Since we visit each node once, it will take O(n) time.

Since the book has no solutions, can anyone please tell if this is correct ?

Also can anyone suggest a solution for second question.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:08:43+00:00Added an answer on June 4, 2026 at 6:08 pm

    First way looks fine to me.

    For the DAG, if you can modify the tree to add cached values to each node, you can use the same algorithm with a small tweak to not recurse if an operator node has a cached value. This should be O(n+m) time (at most one arithmetic operation per node and at most one pointer lookup per edge). Explicitly:

    evaluate(node) {
         if (node has value) {
              return node->value;
         } else {
              left = evaluate(node->left);
              right = evaluate(node->right);
    
              // find operation symbol for node and use it as
              // val = left_val operation right_val
    
              node->value = val;
              return val;
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Skiena's book of algorithm design , given that the hash table has can
A question from Skiena's book of algorithm: Suppose G is a connected undirected graph.
In the book The Algorithm Design Manual by Skiena, computing the mode (most frequent
I've been going through Skiena's excellent The Algorithm Design Manual and got hung up
I'm going through Skiena's The Algorithm Design Manual which uses C. All the examples
Such algorithm was left as an exercise to the reader in Skiena's algorithm design
Hello Developers! I am learning algorithms from Algorithms Design Manual Book by Skiena. There
Got this question from algorithms design manual by Steven Skiena. It is required to
I'm re-reading Skiena's Algorithm Design Manual to catch up on some stuff I've forgotten
I have recently started reading Programming Challenges book by S. Skiena and believe or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.