536. Construct Binary Tree from String 从括号字符串中构建二叉树

[抄题]:

You need to construct a binary tree from a string consisting of parenthesis and integers.

The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root‘s value and a pair of parenthesis contains a child binary tree with the same structure.

You always start to construct the left child node of the parent first if it exists.

Example:

Input: "4(2(3)(1))(6(5))"
Output: return the tree root node representing the following tree:

       4
     /       2     6
   / \   /
  3   1 5

Note:

  1. There will only be ‘(‘‘)‘‘-‘ and ‘0‘ ~ ‘9‘ in the input string.
  2. An empty tree is represented by "" instead of "()".

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

注意下:取数可以多取几位,i+1位是数字时就继续i++

[思维问题]:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 22.0px; font: 15.0px "Helvetica Neue" }

感觉我在背题:几天不背,功力全无。substring都忘了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. new TreeNode(Integer.valueOf(s.substring(j, i + 1)))字符串不能直接转node,需要转interger后再转node
  2. 一直往后移用的是while循环

[二刷]:

  1. new TreeNode(Integer.valueOf(s.substring(j, i + 1))) j的初始值是i,计算之后也应该更新为新的i

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

从i j中截取字符串, j应该跟随i更新

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

for (int i = 0, j = i; i < s.length(); i++, j = i) {
                        TreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
        }

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode str2tree(String s) {
        //corner case
        if (s == null || s.length() == 0) return null;

        //initialization: stack
        Stack<TreeNode> stack = new Stack<TreeNode>();

        //for loop: new node, get substring and append
        for (int i = 0, j = i; i < s.length(); i++, j = i) {
            //get c
            char c = s.charAt(i);

            //if c is )
            if (c == ‘)‘) stack.pop();
            else if ((c >= ‘0‘ && c <= ‘9‘) || (c == ‘-‘)) {
                    //continue
                    while (i + 1 < s.length() && s.charAt(i + 1) >= ‘0‘ && s.charAt(i + 1) <= ‘9‘) i++;
                        //build new node
                        TreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
                        if (!stack.isEmpty()) {
                            TreeNode parent = stack.peek();
                            //get left and append
                            if (parent.left != null) {
                                parent.right = node;
                            }
                            else parent.left = node;
                        }
                        stack.push(node);
                }

        }

        //return the last root
        return stack.peek() == null ? null : stack.pop();
    }
}

原文地址:https://www.cnblogs.com/immiao0319/p/9612471.html

时间: 2024-08-29 20:20:46

536. Construct Binary Tree from String 从括号字符串中构建二叉树的相关文章

[LeetCode] Construct Binary Tree from String 从字符串创建二叉树

You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. SOLUTION 1: 1. Find the root node from the preorder.(it

Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 分析:通过一个二叉树的先序遍历和中序遍历可以确定一个二叉树.先序遍历的第一个元素对应二叉树的根结点,由于在中序遍历中左子树和右子树的中序遍历序列分别在根节点两侧,因此我们可以确定左子树和右子树的中序遍历序列.在先序遍历序列中,

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not