// This function returns overall
        // maximum path sum in 'res' And
        // returns max path sum going through root.
        int findMaxUtil(Node1 node, Res res)
        {
            // Base Case
            if (node == null)
            {
                return(0);
            }

            // l and r store maximum path
            // sum going through left and
            // right child of root respectively
            int l = findMaxUtil(node.left, res);
            int r = findMaxUtil(node.right, res);

            // Max path for parent call of root.
            // This path must include
            // at-most one child of root
            int max_single = Math.Max(Math.Max(l, r) +
                                      node.data, node.data);


            // Max Top represents the sum
            // when the Node under
            // consideration is the root
            // of the maxsum path and no
            // ancestors of root are there
            // in max sum path
            int max_top = Math.Max(max_single,
                                   l + r + node.data);

            // Store the Maximum Result.
            res.val = Math.Max(res.val, max_top);

            return(max_single);
        }
        // Returns maximum path
        // sum in tree with given root
        int findMaxSum(Node1 node)
        {
            // Initialize result
            // int res2 = int.MinValue;
            Res res = new Res();

            res.val = int.MinValue;

            // Compute and return result
            findMaxUtil(node, res);
            return(res.val);
        }
Пример #3
0
        public int dfs(int[,] graph)
        {
            int m = graph.GetLength(0);
            int n = graph.GetLength(1);

            bool[,] visited = new bool[m, n];
            int paths = 0;


            Stack <Node1> s = new Stack <Node1>();

            s.Push(new Node1(0, 0));
            while (s.Count != 0)
            {
                Node1 node = s.Pop();

                if (node.i == m - 1 && node.j == n - 1)
                {
                    paths++;
                    s.Push(new Node1(0, 0));
                    continue;
                }

                if (!visited[node.i, node.j])
                {
                    if (node.i != 0 && node.j != 0)
                    {
                        visited[node.i, node.j] = true;
                    }
                    if (node.i + 1 < m && graph[node.i + 1, node.j] == 0)
                    {
                        s.Push(new Node1(node.i + 1, node.j));
                    }
                    if (node.j + 1 < n && graph[node.i, node.j + 1] == 0)
                    {
                        s.Push(new Node1(node.i, node.j + 1));
                    }
                }
            }

            return(paths);
        }
        /*
         * Write a Program to Find the Maximum Depth or Height of a Tree
         *
         *
         * Given a binary tree, find height of it. Height of empty tree is 0 and height of below tree is 2.
         * 1
         * / \
         * /   \
         * 2     3
         * /\
         * /  \
         * 4    5
         * Recursively calculate height of left and right subtrees of a node and assign height to the node as max of the heights of two children plus 1. See below pseudo code and program for details.
         * Algorithm:
         *
         *
         * maxDepth()
         * 1. If tree is empty then return 0
         * 2. Else
         * (a) Get the max depth of left subtree recursively  i.e.,
         * call maxDepth( tree->left-subtree)
         * (a) Get the max depth of right subtree recursively  i.e.,
         * call maxDepth( tree->right-subtree)
         * (c) Get the max of max depths of left and right
         * subtrees and add 1 to it for the current node.
         * max_depth = max(max dept of left subtree,
         *                   max depth of right subtree)
         + 1
         + (d) Return max_depth
         + See the below diagram for more clarity about execution of the recursive function maxDepth() for above example tree.
         +
         +
         +  maxDepth('1') = max(maxDepth('2'), maxDepth('3')) + 1
         +                     = 1 + 1
         +                        /    \
         +                      /         \
         +                    /             \
         +                  /                 \
         +                /                     \
         +     maxDepth('2') = 1                maxDepth('3') = 0
         + = max(maxDepth('4'), maxDepth('5')) + 1
         + = 1 + 0   = 1
         +         /    \
         +       /        \
         +     /            \
         +   /                \
         + /                    \
         + maxDepth('4') = 0     maxDepth('5') = 0
         +
         */
        int maxDepth(Node1 node)
        {
            if (node == null)
            {
                return(-1);
            }
            else
            {
                /* compute the depth of each subtree */
                int lDepth = maxDepth(node.left);
                int rDepth = maxDepth(node.right);

                /* use the larger one */
                if (lDepth > rDepth)
                {
                    return(lDepth + 1);
                }
                else
                {
                    return(rDepth + 1);
                }
            }
        }
 public Node1(int item)
 {
     data = item;
     left = right = null;
 }