LeetCode题解之Binary Tree Pruning】的更多相关文章

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)…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetcode.com/problems/binary-tree-pruning/description/ 题目描述 We are given the head node root of a binary tree, where additionally every node's value is eith…
题目: 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, 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)中查找此根(…
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* 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 =…
1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); return ans; } int nodesTilt(TreeNode *root, int & ans) { if (root == NULL) ; int tiltleft = nodesTilt(root->left, ans); int tiltright = nodesTilt(roo…