[Java/Python 3] 3 similar recursive and 1 iterative methods w/ comment & analysis.
Three similar recursive and one iterative methods, choose one you like.\n\nMethod 1:\n\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        if (r
Three similar recursive and one iterative methods, choose one you like.\n\n**Method 1:**\n```\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        if (root == null) return 0; // base case.\n        if (root.val < L) return rangeSumBST(root.right, L, R); // left branch excluded.\n        if (root.val > R) return rangeSumBST(root.left, L, R); // right branch excluded.\n        return root.val + rangeSumBST(root.right, L, R) + rangeSumBST(root.left, L, R); // count in both children.\n    }\n```\n```\n    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:\n        if not root:\n            return 0\n        elif root.val < L:\n            return self.rangeSumBST(root.right, L, R)\n        elif root.val > R:\n            return self.rangeSumBST(root.left, L, R)\n        return root.val + self.rangeSumBST(root.left, L, R) + self.rangeSumBST(root.right, L, R)\n```\nThe following are two more similar recursive codes.\n\n**Method 2:**\n```\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        if (root == null) return 0; // base case.\n        return (L <= root.val && root.val <= R ? root.val : 0) + rangeSumBST(root.right, L, R) + rangeSumBST(root.left, L, R);\n    }\n```\n```\n    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:\n        if not root:\n            return 0\n        return self.rangeSumBST(root.left, L, R) + \\\n                self.rangeSumBST(root.right, L, R) + \\\n                (root.val if L <= root.val <= R else 0)\n```\n**Method 3:**\n```\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        if (root == null) { return 0; }\n        int sum = 0;\n        if (root.val > L) { sum += rangeSumBST(root.left, L, R); } // left child is a possible candidate.\n        if (root.val < R) { sum += rangeSumBST(root.right, L, R); } // right child is a possible candidate.\n        if (root.val >= L && root.val <= R) { sum += root.val; } // count root in.\n        return sum;\n    }\n```\n```\n    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:\n        if not root:\n            return 0\n        sum = 0\n        if root.val > L:\n            sum += self.rangeSumBST(root.left, L, R)\n        if root.val < R:\n            sum += self.rangeSumBST(root.right, L, R)\n        if L <= root.val <= R:\n            sum += root.val     \n        return sum\n```\n\n**Method 4: Iterative version**\n```\n    public int rangeSumBST(TreeNode root, int L, int R) {\n        Stack<TreeNode> stk = new Stack<>();\n        stk.push(root);\n        int sum = 0;\n        while (!stk.isEmpty()) {\n            TreeNode n = stk.pop();\n            if (n == null) { continue; }\n            if (n.val > L) { stk.push(n.left); } // left child is a possible candidate.\n            if (n.val < R) { stk.push(n.right); } // right child is a possible candidate.\n            if (L <= n.val && n.val <= R) { sum += n.val; }\n        }\n        return sum;\n    }\n```\n```\n    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:\n        stk, sum = [root], 0\n        while stk:\n            node = stk.pop()\n            if node:\n                if node.val > L:\n                    stk.append(node.left)    \n                if node.val < R:\n                    stk.append(node.right)\n                if L <= node.val <= R:\n                    sum += node.val    \n        return sum\n```\n**Analysis:**\n\nAll 4 methods will DFS traverse all nodes in worst case, and if we count in the recursion trace space cost, the complexities are as follows:\n\n**Time: O(n), space: O(h)**, where `n` is the number of total nodes, `h` is the height of the tree..
['Java', 'Python3']