leetcode 979. 在二叉树中分配硬币

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 示例 3:
  • 示例 4:
  • 解法:

题目描述:

给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币。

在一次移动中,我们可以选择两个相邻的结点,然后将一枚硬币从其中一个结点移动到另一个结点。(移动可以是从父结点到子结点,或者从子结点移动到父结点。)。

返回使每个结点上只有一枚硬币所需的移动次数。

?

示例 1:

输入:[3,0,0]
输出:2
解释:从树的根结点开始,我们将一枚硬币移到它的左子结点上,一枚硬币移到它的右子结点上。

示例 2:

输入:[0,3,0]
输出:3
解释:从根结点的左子结点开始,我们将两枚硬币移到根结点上 [移动两次]。然后,我们把一枚硬币从根结点移到右子结点上。

示例 3:

输入:[1,0,2]
输出:2

示例 4:

输入:[1,0,0,null,3]
输出:4

提示:

  • 1<= N <= 100
  • 0 <= node.val <= N

解法:

/**
 * 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 distributeCoins(TreeNode* root, int& res){
        if(root == NULL){
            return 0;
        }else{
            int left = distributeCoins(root->left, res);
            int right = distributeCoins(root->right, res);
            root->val += left + right;
            res += abs(root->val - 1);
            return root->val - 1;
        }
    }

    int distributeCoins(TreeNode* root) {
        int res = 0;
        distributeCoins(root, res);
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/11064848.html

时间: 2024-08-30 15:49:09

leetcode 979. 在二叉树中分配硬币的相关文章

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | 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 coin from one node to another.  (The move may be from parent to child, or

Leetcode 623.在二叉树中增加一行

在二叉树中增加一行 给定一个二叉树,根节点为第1层,深度为 1.在其第 d 层追加一行值为 v 的节点. 添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树. 将 N 原先的左子树,连接为新节点 v 的左子树:将 N 原先的右子树,连接为新节点 v 的右子树. 如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树. 示例 2: 注意: 输入的深度值 d 的范围是:[1

leetcode:124. 二叉树中的最大路径和

题目描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / 2 3 输出: 6 示例 2: 输入: [-10,9,20,null,null,15,7]   -10    /   9  20     /     15   7 输出: 42 思路分析: 路径问题常规想到的就是用搜索解决.这道题用到了dfs,用递归完成.对于每个结点,计算其左右子树的贡献值,

Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)

题目: 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? 递归解法(C++): /** * Definition for binary tre

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法 2019/5/12Day 9 LeetCode671. Second Minimum Node In a B

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 求二叉树中第k个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

python3实现在二叉树中找出和为某一值的所有路径

在二叉树中找出和为某一值的所有路径请写一个程序创建一棵二叉树,并按照一定规则,输出二叉树根节点到叶子节点的路径.规则如下:1.从最顶端的根结点,到最下面的叶子节点,计算路径通过的所有节点的和,如果与设置的某一值的相同,那么输出这条路径上的所有节点.2.从根节点遍历树时,请请按照左到右遍历,即优先访问左子树的节点.二叉树创建规则:从上到下一层一层的,按照从左到右的顺序进行构造输入"10,5,12,4,7"值,构造的树如下:1) 102) 10      /    5 3) 10