示例#1
0
        public static string Serialize(LeetCode449TreeNode root)
        {
            var sb = PostOrderTraversal(root, new StringBuilder());

            // remove the extra trailer delimiter
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - 1, 1);
            }
            return(sb.ToString());
        }
示例#2
0
        private static StringBuilder PostOrderTraversal(LeetCode449TreeNode root, StringBuilder sb)
        {
            if (root == null)
            {
                return(sb);
            }

            PostOrderTraversal(root.left, sb);
            PostOrderTraversal(root.right, sb);

            sb.Append(root.val);
            sb.Append(' ');
            return(sb);
        }
示例#3
0
        private static LeetCode449TreeNode BuildTree(int lower, int upper, IList <int> nums)
        {
            if (nums.Count == 0)
            {
                return(null);
            }

            var val = nums[nums.Count - 1];

            // make sure this matches the BST definition
            if (val < lower || val > upper)
            {
                return(null);
            }
            // keep processing from the end of list
            nums.RemoveAt(nums.Count - 1);

            var root = new LeetCode449TreeNode(val);

            root.right = BuildTree(val, upper, nums);
            root.left  = BuildTree(lower, val, nums);

            return(root);
        }