由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Find a sub tree with min weight怎么做
相关主题
Find the node with given value in binary tree in in-orderInterview question::
回馈本版,新鲜店面,新题新气象Leetcode bst max path-----is this solution correct?
热腾腾的 LinkedIn 电面题攒RP大虾帮忙看看代码,为什么 res参数传入不了,返回总是null
一道google面试题问题在哪儿啊 kth Node of BST,大家帮忙
讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径发现一个很恶心的基础问题
yelp 电面面经应该已跪了一道在线题
path sum II OJ 超时请问怎么样才能想到关于tree等比较简洁的答案呢?同时记一下超慢速刷题过半以鼓励自己
leetcode的OJ也会有错吗??请教一道g算法题
相关话题的讨论汇总
话题: treenode话题: curlevel话题: weight话题: res话题: tree
进入JobHunting版参与讨论
1 (共1页)
S*******C
发帖数: 822
1
the weight of a tree = sum of weight of all node in the tree. Weight of node
= value of node * level of the node in the tree。
这里的代码能计算weight
public int findWeight(TreeNode root) {
int res = 0;
if (root == null)
return res;
List curLevel = new ArrayList();
curLevel.add(root);
int level = 1;
while (!curLevel.isEmpty()) {
for (TreeNode p : curLevel)
res += p.val * level;
level ++;
List parentLevel = curLevel;
curLevel = new ArrayList();
for (TreeNode p : parentLevel) {
if (p.left != null)
curLevel.add(p.left);
if (p.right != null)
curLevel.add(p.right);
}
}
return res;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一道g算法题讨论Amazon的一道题:根节点到叶节点之间最小和,并打印路径
FB面经yelp 电面面经应该已跪了
求教Leetcode题目:Lowest Common Ancestorpath sum II OJ 超时
渣渣cs本科半应届如何找工作leetcode的OJ也会有错吗??
Find the node with given value in binary tree in in-orderInterview question::
回馈本版,新鲜店面,新题新气象Leetcode bst max path-----is this solution correct?
热腾腾的 LinkedIn 电面题攒RP大虾帮忙看看代码,为什么 res参数传入不了,返回总是null
一道google面试题问题在哪儿啊 kth Node of BST,大家帮忙
相关话题的讨论汇总
话题: treenode话题: curlevel话题: weight话题: res话题: tree