728x90 반응형 Queue2 637. Average of Levels in Binary Tree 이진 트리의 각 레벨에 있는 노드들의 평균 값을 계산하는 문제입니다. 이진 트리의 루트가 주어지면, 각 레벨의 노드들의 평균 값을 배열 형태로 반환해야 합니다.이진 트리의 각 레벨에 있는 노드들의 평균 값을 계산하는 문제입니다. 이진 트리의 루트가 주어지면, 각 레벨의 노드들의 평균 값을 배열 형태로 반환해야 합니다. 실제 답과 10^-5 이내의 오차는 허용됩니다. /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int val=0, TreeNode left=null, TreeNode .. 2024. 4. 18. Sum of nodes at maximum depth of a Binary Tree Recursive: maxDepthSum 함수 : 각 depth에 있는 node들의 합을 구하는 함수 maxDepth 함수에서 while 루프를 통해 최대 depth를 구한다. leaf node의 합을 구하고 싶으므로 , depth-1 을 사용 #include struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; int maxDepthSum(TreeNode* root, int depth) { if (root == nullptr) { return 0; } if (depth == 0) { return root->val; } int leftSum .. 2023. 10. 4. 이전 1 다음 728x90 반응형