import java.util.*;
class Program {
public static int nodeDepths(BinaryTree root) {
// Write your code here.
return findNodeSum(root, 0);
}
public static int findNodeSum(BinaryTree root, int depth){
int sum = 0;
if (root == null){
return 0;
}
sum += depth;
sum += findNodeSum(root.left, depth + 1);
sum += findNodeSum(root.right, depth + 1);
return sum;
}
static class BinaryTree {
int value;
BinaryTree left;
BinaryTree right;
public BinaryTree(int value) {
this.value = value;
left = null;
right = null;
}
}
}
'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글
AlgoExpert Minimum Waiting Time (0) | 2022.07.12 |
---|---|
AlgoExpert Depth First Search (DFS) (0) | 2022.07.12 |
AlgoExpert Sorted Squared Array (0) | 2022.07.10 |
AlgoExpert Non-Constructible Change (0) | 2022.07.10 |
AlgoExpert Tournament Winner (0) | 2022.07.10 |