Easy Java Recursion Solution


  • 0
    L

    Easy Java Recursion Solution

    public class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            return rightSum(root);
        }
        
        private int leftSum(TreeNode node) {
            if (node == null)
                return 0;
            if (node.left == null && node.right == null)
                return node.val;
            return leftSum(node.left) + rightSum(node.right);
        }
        
        private int rightSum(TreeNode node) {
            if (node == null || (node.left == null && node.right == null))
                return 0;
            return leftSum(node.left) + rightSum(node.right);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.