Algorithms/- LeetCode

LeetCode - path sum [Java]

자굿 2022. 1. 30. 21:02
 

Path Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

정답(Solution)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        return dfs(root, 0, targetSum);

    }

    private boolean dfs(TreeNode root, int preSum, int targetSum){

        if(root == null){
            return false;
        }else{
            int nowSum = preSum + root.val;
            if(root.left == null && root.right == null && nowSum == targetSum){
                return true;
            }else{
                return dfs(root.left, nowSum, targetSum) || dfs(root.right, nowSum, targetSum);
            }
        }
    }
}
반응형