606. Construct String from Binary Tree (LeetCode) (solution: C++, C#)

Problem Name: 606. Construct String from Binary Tree 
Problem Link: https://leetcode.com/problems/construct-string-from-binary-tree/
Difficulty: Easy
Tag: String | Tree | Depth First Search | Binary Tree | Graph Theory 
Language: C# | C++
OJ: LeetCode

Algorithm:
1.Base Case:
If the current node root is null, return an empty string.
2.Convert Value:
Convert the value of the current node to a string and store it in the result variable.
3.Left Child:
If the current node has a left child or a right child (or both), recursively call Tree2str on the left child and append the result inside parentheses to the result.
4.Right Child:
If there's a right child, recursively call Tree2str on it as well and append the result inside parentheses to the result.
5.Return Result:
Return the final result string.

Code(C#)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public string Tree2str(TreeNode root) {
        if (root == null) {
            return "";
        }

        string result = root.val.ToString();

        if (root.left != null || root.right != null) {
            result += "(" + Tree2str(root.left) + ")";
        }

        if (root.right != null) {
            result += "(" + Tree2str(root.right) + ")";
        }

        return result;
    }
}
Code(C++)
Upcoming