题解:CF1971D Binary Cut】的更多相关文章

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterati…
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the…
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(…
https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 数组模拟加法操做,注意首位进一的情况. class Solution { public: string addBinary(strin…
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if (root == NULL) return v; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); ; i < size; i++) { TreeNode *node = q.front()…
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(root); return root; } void prun(TreeNode *root) { if (root == NULL) return; if (!has_ones(root->left)) root->left = NULL; if (!has_ones(root->right)…
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) { int size = nums.size(); ) return NULL; TreeNode *dummy = ); maxcont(dummy->left, , size-, nums); return dummy->left; } void maxcont(TreeNode* &…
1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) return ans; leafPath(root,""); return ans; } void leafPath(TreeNode *root, string s) { if (root == NULL) return ; if (root-…
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> ans; if (root == NULL) return ans; queue<TreeNode*> q; q.push(root); vector<int> v; while (!q.empty()) { int size =…