๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] Leetcode 1302 - Deepest Leaves Sum

by applemango2021 2021. 4. 11.

leetcode.com/problems/deepest-leaves-sum/

 

Deepest Leaves 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

๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ value๊ฐ’์„ ํ•ฉํ•˜๋Š” ๋ฌธ์ œ๋‹ค. 

'์ฒ˜์Œ๋ถ€ํ„ฐ ๋„ˆ๋ฌด ๋งŽ์€ ์ƒ๊ฐ์„ ํ•˜์ง€ ๋ง์ž!'๊ณ  ๊ฒฐ์‹ฌํ•˜๊ณ  ์ •๋ง ๋– ์˜ค๋ฅด๋Š” ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋”๋‹ˆ ํ†ต๊ณผ๋Š” ํ–ˆ๋Š”๋ฐ ์• ์ž”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค ใ…‹ใ…‹ใ…‹


Solution 

๋‚˜๋ฆ„์˜ BFS๋กœ ๊ฐ€์žฅ ์•„๋ž˜์˜ ๋…ธ๋“œ๊ฐ€ ๋ช‡ depth์— ์žˆ๋Š”์ง€ ์ฐพ๊ณ , ๋‹ค์‹œ ํ•œ๋ฒˆ Queue๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ ํ•ด๋‹น depth์— ์†ํ•˜๋Š” ๋…ธ๋“œ๋“ค์˜ val์„ ๋”ํ•˜๋Š” ํ’€์ด๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

/**
 * 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 int deepestLeavesSum(TreeNode root) {
        int depth = 0;

        Queue<Node> que = new LinkedList<>();
        que.add(new Node(root, 0));

        Node tmp;
        TreeNode right_node;
        TreeNode left_node;
        while(!que.isEmpty()){
            tmp = que.poll();
            depth = Math.max(depth, tmp.cnt);

            right_node = tmp.treeNode.right;
            if(right_node !=null) que.add(new Node(right_node,tmp.cnt+1));
            left_node = tmp.treeNode.left;
            if(left_node !=null) que.add(new Node(left_node,tmp.cnt+1));
        }

        //while์—์„œ ๋น ์ ธ๋‚˜์˜จ depth๊ฐ€ ๊ฐ€์žฅ ๊นŠ์€ ๊ธธ์ด
        que.clear();
        que.add(new Node(root, 0));
        int total_sum = 0;
        while(!que.isEmpty()){
            tmp = que.poll();

            if(tmp.cnt==depth) total_sum += tmp.treeNode.val;
            right_node = tmp.treeNode.right;
            if(right_node !=null) que.add(new Node(right_node,tmp.cnt+1));
            left_node = tmp.treeNode.left;
            if(left_node !=null) que.add(new Node(left_node,tmp.cnt+1));

        }


        return total_sum;        
    }
    
    
    public static class Node{
        TreeNode treeNode;
        int cnt;

        public Node(TreeNode treeNode, int cnt){
            this.treeNode = treeNode;
            this.cnt = cnt;
        }
    }    
}

 

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ Solution 

leetcode.com/problems/deepest-leaves-sum/discuss/463239/JavaC%2B%2BPython-Level-Traversal

 

[Java/C++/Python] Level Traversal - LeetCode Discuss

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

ใ„ฑ ใ…ฃ๋˜ฅ์ฐจ๋‹ค ์ •๋ง

๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ์—๋Š” ๋‚ด right๊ณผ left๊ฐ€ ๋ชจ๋‘ null์ž„์„ ์ด์šฉํ•˜์—ฌ์„œ ํ’€์—ˆ๋‹ค. ์ฒ˜์Œ ๋‚˜๋„ ์ด ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ๋‹ค๊ฐ€ ์•„๋ž˜์˜ Node 5์™€ ๊ฐ™์ด ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋Š” ์•„๋‹Œ๋ฐ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š”๋ฐ ์–ด๋–กํ•˜์ง€? ๋ผ๋Š” ์ƒ๊ฐ์— ๊ฐ€์žฅ ๋‹จ์ˆœ ๋ฌด์‹ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ธฐ๋กœ ๋Œ๋ฆฐ ๊ฑฐ์˜€๋Š”๋ฐ que.size()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋์—ˆ๋˜ ๊ฑฐ์˜€๋‹ค.

 ์–ด์ฐจํ”ผ root๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉด์„œ ๊ฐ™์€ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค์€ while๋ฌธ ์•ˆ์—์„œ ๊ฐ™์ด que์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜์–ด์žˆ๋‹ค. ์ฆ‰, que.size()๋กœ ๋‚˜์˜ค๋Š” ๊ฐ’์ด ๊ฐ™์€ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ ๊ฐฏ์ˆ˜์ธ ๊ฒƒ. ๊ทธ ํšŸ์ˆ˜๋งŒํผ์„ for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ total_sum์— ๋”ํ•ด์ฃผ๋ฉด ๊ฐ’์€ ๋‚˜์˜จ๋‹ค. ์•„์ด๋””์–ด๋ฅผ ์ฐธ๊ณ ํ•œ ์‚ฌ๋žŒ์˜ ๊ฒฝ์šฐ์—๋Š”, for๋ฌธ์—์„œ ์•„์˜ˆ res(๋‚ด ์ฝ”๋“œ๋กœ ์น˜๋ฉด total_sum)์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋˜๋ฐ ์™€ ์ด๊ฒƒ๋„ ๋‚˜ํ•œํ… ์‹ ๋ฐ•ํ•˜๋‹ค. 

/**
 * 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 int deepestLeavesSum(TreeNode root) {
        int total_sum = 0;

        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        while (!que.isEmpty()){
            total_sum = 0;
            for(int i= que.size()-1;i>=0;i--){
                TreeNode node = que.poll();
                total_sum += node.val;

                if(node.left!=null) que.add(node.left);
                if(node.right!=null) que.add(node.right);
            }
        }
        return total_sum;   
    }
   
}