Example #1
0
#
# Given a binary tree, find its maximum depth.
#
# The maximum depth is the number of nodes along the longest path from the root
# node down to the farthest leaf node.
#

import argparse
from helper import get_tree_from_list


class Solution:
    def max_depth(self, root):
        if root is None:
            return 0
        return max(self.max_depth(root.left), self.max_depth(root.right)) + 1


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--tree',
                        dest='tree',
                        required=True,
                        nargs='+',
                        type=int,
                        help='list, represent a breadth first traversal tree')

    args = parser.parse_args()
    root_node = get_tree_from_list(args.tree)
    print(Solution().max_depth(root_node))
import argparse
from helper import get_tree_from_list


class Solution(object):
    def is_valid_bst(self, root):
        return self.is_valid_bst_recursive(root, float("-inf"), float("inf"))

    def is_valid_bst_recursive(self, node, low, high):
        if node is None:
            return True

        return low < node.val < high \
            and self.is_valid_bst_recursive(node.left, low, node.val) \
            and self.is_valid_bst_recursive(node.right, node.val, high)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--tree',
                        dest='tree',
                        required=True,
                        nargs='+',
                        help='list, represent a breadth first traversal tree')

    args = parser.parse_args()
    root_node = get_tree_from_list([int(i) for i in args.tree])

    print(Solution().is_valid_bst(root_node))