想象有一只青蛙要过河。河面用一个一维 0/1 数组表示,0 表示这一格是水,1 表示这一格是石头。这只青蛙不会游泳,但可以在石头之间跳跃,不过要满足:假设上一次跳跃的距离为d,这次跳跃的距离必须是 d-1、d 和 d-1 之一的数值。给定这个一维数组,请编程确定这只青蛙是否能顺利过河?注意跳跃的距离不能为负,也就是说不能回头。
这道题目也是Leetcode的403道题目:https://leetcode.com/problems/frog-jump/description/
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
Example 1: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 28Output: False
Print a binary tree in an m*n 2D string array following these rules:
The row number m should be equal to the height of the given binary tree. The column number n should always be an odd number. The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them. Each unused space should contain an empty string “”. Print the subtrees following the same rules.
Example 1:
Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]]Example 2:
Input: 1 / \ 2 3 \ 4 Output: [["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]]Example 3:
Input: 1 / \ 2 5 / 3 / 4 Output: [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] Note: The height of binary tree is in the range of [1, 10].Calculate depth and use recursive to store respond elements.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int findDepth(TreeNode* root) { if(!root) { return 0; } int left_d = findDepth(root->left) + 1; int right_d = findDepth(root->right) + 1; return left_d > right_d?left_d:right_d; } void insertNumber(vector<vector<string>>&res, TreeNode* root, int low, int high, int layer) { if(!root) { return; } int idx = (low+high) >> 1; res[layer][idx] = to_string(root->val); insertNumber(res, root->left, low, idx - 1, layer + 1); insertNumber(res, root->right, idx + 1, high, layer + 1); } vector<vector<string>> printTree(TreeNode* root) { if(!root) { vector<vector<string>> no_ele; return no_ele; } int depth = findDepth(root); int num = exp2(depth); vector<vector<string>> res(depth, vector<string>(num-1, "")); insertNumber(res, root, 0, num-1, 0); return res; } };