public static string Tree2Str(LeetCode606TreeNode root) { if (root == null) { return(string.Empty); } if (root.IsLeafNode()) { return(root.val + ""); } if (!root.HasRightChild()) { return($"{root.val}({Tree2Str(root.left)})"); } // since this is the binary tree, so if it has right child it must have left child return($"{root.val}({Tree2Str(root.left)})({Tree2Str(root.right)})"); }
public LeetCode606TreeNode(int val = 0, LeetCode606TreeNode left = null, LeetCode606TreeNode right = null) { this.val = val; this.left = left; this.right = right; }