成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 663. Equal Tree Partition

coordinate35 / 1480人閱讀

Problem

Given a binary tree with n nodes, your task is to check if it"s possible to partition the tree to two trees which have the equal sum of values after removing exactly one edge on the original tree.

Example 1:

Input:     
    5
   / 
  10 10
    /  
   2   3

Output: True
Explanation: 
    5
   / 
  10
      
Sum: 15

   10
  /  
 2    3

Sum: 15

Example 2:

Input:     
    1
   / 
  2  10
    /  
   2   20

Output: False
Explanation: You can"t split the tree into two trees with equal sum after removing exactly one edge on the tree.

Note:
The range of tree node value is in the range of [-100000, 100000].
1 <= n <= 10000

Solution
class Solution {
    Map map = new HashMap<>();
    public boolean checkEqualTree(TreeNode root) {
        if (root == null) return false;
        int total = getTotal(root);
        if (total%2 != 0) return false;
        if (total/2 == 0) return map.getOrDefault(total/2, 0) > 1;
        return map.containsKey(total/2);
    }
    private int getTotal(TreeNode root) {
        if (root == null) return 0;
        int total = root.val+getTotal(root.left)+getTotal(root.right);
        map.put(total, map.getOrDefault(total, 0)+1);
        return total;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/72360.html

相關(guān)文章

  • [Leetcode - Dynamic Programming] Partition Equal S

    摘要:背包問題假設(shè)有個(gè)寶石,只有一個(gè)容量為的背包,且第個(gè)寶石所對(duì)應(yīng)的重量和價(jià)值為和求裝哪些寶石可以獲得最大的價(jià)值收益思路我們將個(gè)寶石進(jìn)行編號(hào),尋找的狀態(tài)和狀態(tài)轉(zhuǎn)移方程。我們用表示將前個(gè)寶石裝到剩余容量為的背包中,那么久很容易得到狀態(tài)轉(zhuǎn)移方程了。 Partition Equal Subset Sum Given a non-empty array containing only posi...

    qpal 評(píng)論0 收藏0
  • leetcode416. Partition Equal Subset Sum

    摘要:題目要求假設(shè)有一個(gè)全為正整數(shù)的非空數(shù)組,將其中的數(shù)字分為兩部分,確保兩部分?jǐn)?shù)字的和相等。而這里的問題等價(jià)于,有個(gè)物品,每個(gè)物品承重為,問如何挑選物品,使得背包的承重搞好為所有物品重量和的一般。 題目要求 Given a non-empty array containing only positive integers, find if the array can be partitio...

    Caicloud 評(píng)論0 收藏0
  • [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether its possible to divide this array into k non-empty subsets whose sums are all equal. Example 1:Input: nums = [4, 3, 2, 3,...

    kuangcaibao 評(píng)論0 收藏0
  • [LeetCode] 416. Partition Equal Subset Sum

    Problem Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note:Each of the array ...

    makeFoxPlay 評(píng)論0 收藏0
  • leetcode 416 Partition Equal Subset Sum

    摘要:如果是奇數(shù)的話,那一定是不滿足條件的,可以直接返回。如果是偶數(shù),將除獲得我們要求的一個(gè)子數(shù)組元素的和。如何暫存我們計(jì)算的中間結(jié)果呢聲明一個(gè)長(zhǎng)度為的布爾值數(shù)組。每個(gè)元素的布爾值代表著,數(shù)組中是否存在滿足加和為的元素序列。 題目詳情 Given a non-empty array containing only positive integers, find if the array ca...

    jsummer 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<