C# solution using BFS


  • 0
    W

    use flag to indicate it is left or right, then it will be really easy to use BFS to solve this answer

        public int SumOfLeftLeaves(TreeNode root)
        {
            return Helper(root, false);
        }
    
        private int Helper(TreeNode root, bool isleft)
        {
            if (root == null) return 0;
    
            // if it is left child
            if(isleft && root.left == null && root.right == null)
            {
                return root.val;
            }
    
            return Helper(root.left, true) + Helper(root.right, false);
        }

Log in to reply
 

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