Java build tree and dfs
I really like this question, very suitable for real interview.\nTesting tree building, dfs, corner case, etc\n\n\nclass Solution {\n    long max = 0;\n    long 
I really like this question, very suitable for real interview.\nTesting tree building, dfs, corner case, etc\n\n```\nclass Solution {\n    long max = 0;\n    long cnt = 0;\n    public int countHighestScoreNodes(int[] parents) {\n        TreeNode root = buildTree(parents); \n        dfs(root, parents.length);\n        return (int) cnt;\n    }\n    \n    private long dfs(TreeNode node, int n) {\n        if (node == null) return 0;\n        long l = dfs(node.left, n);\n        long r = dfs(node.right, n);\n        long remain = n - l - r - 1;\n        long prod = (l > 0 ? l : 1) * (r > 0 ? r : 1) * (remain > 0 ? remain : 1); // tricky\n        if (prod == max) {\n            cnt++;\n        } else if (prod > max) {\n            max = prod;\n            cnt = 1;\n        }\n        return l + r + 1;\n    }\n    \n    private TreeNode buildTree(int[] parents) {\n        int n = parents.length;\n        TreeNode[] nodeArray = new TreeNode[n];\n        TreeNode root = null;\n        \n        for (int i = 0; i < n; i++) {\n            nodeArray[i] = new TreeNode(i);\n        }\n        \n        for (int i = 0; i < n; i++) {\n            if (parents[i] == -1) {\n                root = nodeArray[i];\n                continue;\n            }\n            TreeNode node = nodeArray[parents[i]]; // i = 1, node = 2\n            if (node.left == null) {\n                node.left = nodeArray[i];\n            } else {\n                node.right = nodeArray[i];\n            }\n\n        }\n        return root;\n    }\n}\n\nclass TreeNode {\n    int val;\n    TreeNode left;\n    TreeNode right;\n    TreeNode(int val) {\n        this.val = val;\n    }\n}\n```
[]