2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送多余的硬币数量,不论正负,都是需要占用move数量的.自此递归的进行计数即可. public int distributeCoins(TreeNode root) { int[] res = new int[1]; helper(root, res); return res[0]; } privat…
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or…
Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目描述 Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there…
原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one…
题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are Ncoins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child…
问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移动,我们只能向相邻接点移动,也就是说coins 只能向父节点或者子节点移动,那么求最终需要移动多少步. Leetcode原题链接: https://leetcode.com/problems/distribute-coins-in-binary-tree/ 分析 如果叶子节点有0个coins,那么我…
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 retur…
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. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7…
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and doe…
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs t…
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level…
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique valu…
[抄题]: 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) [思维问题]: 不会写分合法 [一句话思路]: 用两次分治:root2any any2any分一次,左右再分一次. [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: 先root-any左右各一次,再用any-any. [一刷]: left right都是resultType类型,要用到helper函数…
就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,自底向上的返回他的层次遍历的节点的values.(提示,从左到右,一层一层的遍历,但是这里是从叶子节点到根节点) 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回的层次遍历的结果是: [ [15,7] [9,…
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的后序遍历的节点的values. 例如: 给定一个二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1]. 笔记: 递归解决方案是微不足道的,你可以用迭代的方法吗? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++…
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的中序遍历的节点的values. 例如: 给定一个二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 笔记: 递归解决方案是微不足道的,你可以用迭代的方法吗? 困惑什么"{1,#,2,3}" 的意思吗? > read more on how binary tree is s…
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). LeetCode103. Binary Tree Zigzag Level Order Traversal中等 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回锯齿形层次遍历如…
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. LeetCode104. Maximum Depth of Binary Tree 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3. Java 实现 class TreeNode…
题目描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路 二叉树转化为链表的基本思想是:对于左孩子转化为右孩子:对于右孩子,拼接到根结点左子树最后一个节点作为右孩子.所以在自上而下转化时,对于每个节点要先保存其右孩子,然后记录转为链表后本子树的最后一个节点并返回给上一个根节点. 代码 /** * Definition for a binary tree node. * stru…
题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后序遍历的顺序是左孩子->右孩子->父节点,对于每个遍历到的节点,执行如下操作: 首先对该节点不断沿左孩子方向向下遍历并入栈,直到左孩子为空 取出栈顶节点,此时该节点为树的最左下节点,若其右孩子不为空,则回到步骤1:若为空说明其为叶子节点,将其值输出到结果中,并记录pre为当前节点 在步骤2判断右孩…
题目描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由于中序遍历的顺序是左孩子->父节点->右孩子,所以在遍历到某节点时,先依次把所有左孩子入栈,找到最左边的子节点后,输出该节点,然后继续遍历该节点的右孩子. 代码 /** * Definition for a binary tree node. * struct TreeNode { * int va…
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (<=20) which is the total nu…
[抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型控制变量 [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: root一个点正常出入.第二层先左后右,出来就变成先右后左了. [一刷]: 层遍历是放在原来的queue中,但是zigzag遍历的左右节点是放在下一层,另外一个栈…
2018-07-27 15:55:13 问题描述: 问题求解: 题目中说明了最后的宽度计算其实是按照满二叉树来进行计算的,也就是说如果我们能够得到每层最左边的节点编号和最右边的节点编号,那么本题就可以进行解决了. 另外,在如何编号的问题上,既然是满二叉树,那么编号的方式自然是父节点i,左子节点2 * i,右子节点2 * i + 1. public int widthOfBinaryTree(TreeNode root) { return helper(root, 0, 1, new ArrayL…
求二叉树的最大深度 /** * 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 maxDepth(TreeNode* root) { int l,r; ; l…
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: R…
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o…
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inorder Traversal 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? Java 实现 Iterative Solution import java.util.LinkedList; import java…
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree Postorder Traversal 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? Java 实现 Iterative Solution import java.util.LinkedList; impo…