问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移动,我们只能向相邻接点移动,也就是说coins 只能向父节点或者子节点移动,那么求最终需要移动多少步. Leetcode原题链接: https://leetcode.com/problems/distribute-coins-in-binary-tree/ 分析 如果叶子节点有0个coins,那么我…
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…
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…
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL…
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要在左右子树中取较大的就行了. 然而最小深度时,如果左右子树中有一个为空会返回0,这时我们是不能算做有效深度的. 所以分成了三种情况,左子树为空,右子树为空,左右子树都不为空.当然,如果左右子树都为空的话,就会返回1. 广度优先搜索(类似层序遍历的思想) 递归解法本质是深度优先搜索,但因为我们是求最小…
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 给一个二叉树,找出它的最小深度.最小深度是从根节点向下到最近的叶节点的最短路径,就是最短路径的节点个数. 解法1:DFS 解法2: BFS Java: DFS, Time Comp…