[LeetCode] 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 1:

Input: s = "4(2(3)(1))(6(5))"
Output: [4,2,6,3,1,5]

Example 2:

Input: s = "4(2(3)(1))(6(5)(7))"
Output: [4,2,6,3,1,5,7]

Example 3:

Input: s = "-4(2(3)(1))(6(5)(7))"
Output: [-4,2,6,3,1,5,7]

Constraints:

  • 0 <= s.length <= 3 * 104
  • s consists of digits, '('')', and '-' only.

从字符串生成二叉树。

你需要从一个包括括号和整数的字符串构建一棵二叉树。

输入的字符串代表一棵二叉树。它包括整数和随后的 0 ,1 或 2 对括号。整数代表根的值,一对括号内表示同样结构的子树。

若存在左子结点,则从左子结点开始构建。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用栈。仔细观察你会发现input字符串里面节点值val实际是按前序遍历给的。首先排除corner case比如input字符串为空的情况,就直接返回NULL。对于一般情况而言,我们首先一点点构造节点值,这里同时注意,节点值有可能是负的。构造出来节点的值之后,此时如果栈不为空,则说明此时构造的节点是栈顶元素的左孩子或者右孩子。这里要优先构造左孩子。指针连好之后,把节点入栈。

遇到左括号不做任何动作,但是遇到右括号则把栈顶元素弹出。最后理想情况下,栈里面只会剩下根节点;若栈为空,则return null。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public TreeNode str2tree(String s) {
18         Stack<TreeNode> stack = new Stack<>();
19         for (int i = 0; i < s.length(); i++) {
20             // 左括号,不作任何动作
21             if (s.charAt(i) == '(') {
22                 continue;
23             }
24             // 右括号,弹出栈顶元素
25             // 因为input里括号的结束代表当前子树已经遍历完了
26             else if (s.charAt(i) == ')') {
27                 stack.pop();
28             }
29             // 构建数字
30             else if (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '-') {
31                 int start = i;
32                 while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') {
33                     i++;
34                 }
35                 TreeNode root = new TreeNode(Integer.valueOf(s.substring(start, i + 1)));
36                 // 如果栈不为空,那么栈顶元素就是当前节点的父亲
37                 if (!stack.isEmpty()) {
38                     TreeNode parent = stack.peek();
39                     if (parent.left == null) {
40                         parent.left = root;
41                     } else {
42                         parent.right = root;
43                     }
44                 }
45                 stack.push(root);
46             }
47         }
48         if (stack.isEmpty()) {
49             return null;
50         }
51         return stack.peek();
52     }
53 }

 

相关题目

297. Serialize and Deserialize Binary Tree

449. Serialize and Deserialize BST

536. Construct Binary Tree from String

606. Construct String from Binary Tree

LeetCode 题目总结

posted @ 2020-10-21 02:14  CNoodle  阅读(320)  评论(0编辑  收藏  举报