C# Solution: three recursions: left, leaves and right.


  • 0
    B
    public class Solution 
    {
        public IList<int> BoundaryOfBinaryTree(TreeNode root) 
        {
            var result = new List<int>();
            if (root == null) return result;
            if (root.left == null && root.right == null)
            {
                result.Add(root.val);
                return result;
            }
            result.Add(root.val);
    
            Left(root.left, result);
            Leaves(root, result);
            Right(root.right, result);
            return result;
        }
    
        private void Left(TreeNode root,  IList<int> result)
        {
            if (root == null) return;
            if (root.left == null && root.right == null) return;
    
            result.Add(root.val);
    
            if (root.left != null) Left(root.left, result);
            else                   Left(root.right, result);
        }
    
        private void Leaves(TreeNode root, IList<int> result)
        {
            if (root == null) return ;
            if (root.left == null && root.right == null)
            {
                result.Add(root.val);
            }
    
            Leaves(root.left, result);
            Leaves(root.right, result); 
        }
    
        private void Right(TreeNode root, IList<int> result)
        {
            if (root == null) return;
    
            if (root.left == null && root.right == null) return;
    
            var right = root;
            if (root.right != null) Right(root.right, result);
            else if (root.right == null) Right(root.left, result);
    
            result.Add(right.val);
        }
    }
    

Log in to reply
 

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