The solution is based on postorder.
By the implementation in binary tree traversal,
we could easily solve this problem by replace Run
function
from printing node's value to calculating the sum of the children node's value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#include <stack> | |
template<class T> | |
class Queue | |
{ | |
public: | |
Queue() | |
{ |
- Implement Morris's traversal algorithms.
- Implement leetcode's solution for postorder.
- Add comment to explain the implementation by using visited-flag(flag.cpp)
- Non-recursive Preorder Traversal of Binary Tree
- Tushar Roy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#include <string> | |
bool | |
IsHalfEqual(std::string s) | |
{ | |
assert(s.length() % 2 == 0); | |
unsigned int i = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ g++ minPrice1.cpp --std=c++11 | |
// | |
// Ticket Prices: | |
// | |
// To | |
// | 0 | 1 | 2 | 3 | 4 | | |
// --+-----+-----+-----+-----+-----+ | |
// 0 | 0 | 10 | 75 | 94 | 107 | | |
// F --+-----+-----+-----+-----+-----+ | |
// r 1 | * | 0 | 35 | 50 | 83 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile by $ g++ hanoi.cpp --std=c++11 | |
// | |
// The standard `Tower of Hanoi` has 3 pegs and a number of disks | |
// of different sizes, which can slide onto any peg. | |
// The objective of the puzzle is to move entire stack from one peg to | |
// another peg, obeying the following simple rules: | |
// 1. The disks need to keep in ascending order of size on the peg any time. | |
// That is, no disk can be placed on top of a smaller disk. | |
// 2. Only one disk can be moved at a time. | |
// 3. Each move consists of taking the upper disk from one of the stacks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <iostream> | |
#undef RECURSION | |
#undef DYNAMIC_PROGRAMMING | |
#undef RUN | |
#define RECURSION 1 | |
#define DYNAMIC_PROGRAMMING 2 | |
#define RUN RECURSION |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "exp.h" | |
#include <stack> | |
// Calculate the exponentiation by squaring: | |
// k ^ n = (k ^ (n / 2)) ^ 2 if n is even | |
// or k * (k ^ ((n - 1) / 2))^ 2 if n is odd | |
// or | |
// k ^ n = (k ^ 2) ^ (n / 2) if n is even | |
// or k * (k ^ 2) ^ ((n - 1) / 2) if n is odd | |
// |
Implementation of different methods to calculate the Fibonacci numbers by fast doubling.
Please read my blog post for more detail.
- RefPtr
- Add more tests