【英雄算法六月集训】Day14

【英雄算法六月集训】Day14

508. 出现次数最多的子树元素和

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();

    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int sum = root.val;
        sum += dfs(root.left);
        sum += dfs(root.right);
        cnt.put(sum, cnt.getOrDefault(sum, 0) + 1);
        return sum;
    }

    public int[] findFrequentTreeSum(TreeNode root) {

        List<int[]> ans = new ArrayList<int[]>();
        List<Integer> ret = new ArrayList<Integer>();
        dfs(root);
        for (Integer key : cnt.keySet()) {
            int[] tmp = new int[2];
            tmp[0] = cnt.get(key);
            tmp[1] = key;
            ans.add(tmp);
        }
        ans.sort((a,b)->{
            return b[0]-a[0];
        });

        int[] tmp = ans.get(0);
        ret.add(tmp[1]);

        for (int i = 1; i < ans.size(); ++i) {
            int[] a = ans.get(i);
            int[] b = ans.get(i-1);
            if (a[0] == b[0])
                ret.add(a[1]);
            else break;
        }

        return ret.stream().mapToInt(Integer::valueOf).toArray();
    }
}

1600. 王位继承顺序](https://leetcode.cn/problems/throne-inheritance/)

class Tree{
    String s;
    boolean isDead;
    List<Tree> childList;

    Tree(){
        s = "";
        isDead = false;
        childList = new ArrayList<Tree>();
    }
}
class ThroneInheritance {
    Map<String,Tree> hash = new HashMap<String,Tree>();
    Tree root;
    public ThroneInheritance(String kingName) {
        root = newNode(kingName);
    }
    public Tree newNode(String s){
        Tree n = new Tree();
        n.s = s;
        hash.put(s,n);
        return n;
    }
    public void birth(String parentName, String childName) {
        Tree node = hash.get(parentName);
        node.childList.add(newNode(childName));
    }
    
    public void death(String name) {
        hash.get(name).isDead  = true;
    }
    public void dfs(Tree root,List<String> ret){
        if(root!=null){
            if(!root.isDead){
                ret.add(root.s);
            }
            for(int i=0;i <root.childList.size();++i){
               dfs(root.childList.get(i),ret);
            }
        }
    }
    public List<String> getInheritanceOrder() {
        List<String> ret= new ArrayList<String>();
        dfs(root,ret);
        return ret;
    }
}

/**
 * Your ThroneInheritance object will be instantiated and called as such:
 * ThroneInheritance obj = new ThroneInheritance(kingName);
 * obj.birth(parentName,childName);
 * obj.death(name);
 * List<String> param_3 = obj.getInheritanceOrder();
 */

1026. 节点与其祖先之间的最大差值

class Solution {
    public int maxAncestorDiff(TreeNode root) {
        int left = maxAncestorDiff(root.left, root.val, root.val);
        int right = maxAncestorDiff(root.right, root.val, root.val);
        return left > right ? left : right;
    }
    
    public int maxAncestorDiff(TreeNode root, int max, int min){
        if(root == null){
            return 0;
        }
        if(root.val > max){
            max = root.val;
        }
        else if(root.val < min){
            min = root.val;
        }
        if(root.left == null && root.right == null){
            return max - min;
        }
        int left = maxAncestorDiff(root.left, max, min);
        int right = maxAncestorDiff(root.right, max, min);
        return left > right ? left : right;
    }
}

655. 输出二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int getTreeHight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getTreeHight(root.left), getTreeHight(root.right))+ 1;
    }

    void dfs(int H, int d, int p, TreeNode root, List<List<String>> ret) {
        if (root == null) {
            return;
        }
        int linenum = p - (1 << d);
        int first = (1 << (H - 1 - d)) - 1;
        int command = 1 << (H - 1 - d + 1);
        int col = first + (linenum) * command;
        int row = d;
        ret.get(row).set(col, root.val + "");
        dfs(H, d + 1, p << 1, root.left, ret);
        dfs(H, d + 1, p << 1 | 1, root.right, ret);
    }

    public List<List<String>> printTree(TreeNode root) {
        //获取到树的高度
        int h = getTreeHight(root);
        int n = (1 << h) - 1;
        int m = h;
        List<List<String>> ret = new ArrayList<List<String>>();
        for (int i = 0; i < m; i++) {
            List<String> ans = new ArrayList<String>();
            for (int j = 0; j < n; ++j) {
                ans.add("");
            }
            ret.add(ans);
        }
        dfs(h, 0, 1, root, ret);
        return ret;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值