본문 바로가기
728x90
반응형

재귀3

21. Merge Two Sorted Lists 주어진 두 개의 정렬된 연결 리스트를 하나의 정렬된 리스트로 병합하는 문제입니다. 첫 번째 두 리스트의 노드를 이어 붙여서 새로운 정렬된 연결 리스트를 만들어야 합니다. 병합된 연결 리스트의 헤드 노드를 반환해야 합니다. /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode MergeTwoLists(ListNode li.. 2024. 4. 23.
112. Path Sum 주어진 이진 트리에서 루트부터 리프까지의 경로 중 합이 특정 값과 같은 경로가 있는지 확인하는 문제입니다. 간단히 말하면, 주어진 트리에서 어떤 경로를 따라 이동하면서 노드의 값을 더했을 때, 그 합이 주어진 값과 같은지를 확인하는 것입니다. /** * 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 right=null) { * this.val = val; * this.left = left; * this.right = rig.. 2024. 4. 23.
hasPathWithGivenSum 이진 트리에서 특정 합계를 갖는 경로가 있는지 확인하는 문제를 해결하는 함수입니다. 주어진 이진 트리는 각 노드가 정수 값을 가지고 있습니다. // // Binary trees are already defined with this interface: // class Tree { // public T value { get; set; } // public Tree left { get; set; } // public Tree right { get; set; } // } bool solution(Tree t, int s) { return hasPathWithGivenSum(t,s); } bool hasPathWithGivenSum(Tree t, int s){ if(t == null) return false; i.. 2024. 4. 2.
728x90
반응형