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.

算法分析:

在 class Solution 内声明一个 int index=0,用来标记当前应该考察的输入字符串的下标,如果下标所指示的是数字,则读取数字,并增进下标;如果下标所指示的是'(',则需要递归调用生成TreeNode的函数,并将结果放在root.left子树中;如果当前字符为')',则当前递归调用结束,将 index 增加 1 ,并返回生成的 TreeNode。

在父级的函数调用中,将返回的TreeNode放在root.left,考差当前 index 所指示的字符,如果为'(',则继续递归调用函数来生成右子树;如果为')',说明当前子树没有右子树,将 index 增加 1,并返回当前TreeNode。

Java 算法实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int index=0;
public TreeNode str2tree(String s) {
int len=s.length();
if(len<1||index>=len){
return null;
}
int val=0;
int ch;
int sign=1;
if(s.charAt(index)=='-'){
sign=-1;
index++;
}
while(index<len&&(ch=s.charAt(index))<='9'&&ch>='0'){
val*=10;
val+=ch-'0';
index++;
}
TreeNode root=new TreeNode(sign*val);
if(index>=len||s.charAt(index)==')'){
index++;
return root;//have no child
}//here now index is pointing to a '(' index++;//now pointing to a number
root.left=str2tree(s);
if(index>=len||s.charAt(index)==')'){
index++;
return root;
}//here it means index is pointing to '('
index++;
root.right=str2tree(s);
if(index>=len||s.charAt(index)==')'){
index++;
}
return root;
}
}

LeetCode 536----Construct Binary Tree from String的更多相关文章

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

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

  2. 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...

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

    [抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...

  4. (二叉树 递归) 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 ...

  5. (二叉树 递归) leetcode 106. 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 ...

  6. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. [LeetCode] 106. 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 ...

  8. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  9. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  10. [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

随机推荐

  1. 实验三:分别用for、while和do-while循环语句以及递归方法计算n!,并输出算式

    一.用for循环计算n! package for_package; import java.util.*;//导入含有输入类的包 public class for_class { /** * @par ...

  2. 死锁、Lock锁、等待唤醒机制、线程组、线程池、定时器、单例设计模式_DAY24

    1:线程(理解) (1)死锁 概念: 同步中,多个线程使用多把锁之间存在等待的现象. 原因分析: a.线程1将锁1锁住,线程2将锁2锁住,而线程1要继续执行锁2中的代码,线程2要继续执行锁1中的代码, ...

  3. 如何在NAS上安装Git Server

    前段时间一时兴起,买了一个NAS,具体型号是QNAP TS-269L.一方面用作硬盘存储数据,另一方面为了方便就在上面搭了一个Git代码服务器.下面详述一下这个Git Server是如何搭建起来的. ...

  4. 【数组】Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. web前端html快速入门

    HTML 学前端之间不得不知道一个网站:http://www.w3school.com.cn/ 网上有很多教程关于前端的,写的特别详细,也写的特别好.我们应该要自已理解,一些相应的前端的知识,不能只是 ...

  6. Word2vec 理解

    1.有DNN做的word2vec,取隐藏层到softmax层的权重为词向量,softmax层的叶子节点数为词汇表大小 2-3的最开始的词向量是随机初始化的 2.哈夫曼树:左边走 sigmoid(当前节 ...

  7. freepbx13通话无声音通话自动挂断

    在阿里云上用脚本一键搭建好了freepbx13,但是在创建好sip分机之后,打电话没有声音,双方都听不到对方的声音.这个是nat问题. 这里有个坑我提醒下大家,就是我们最好不要用台式电脑进行测试通话. ...

  8. 电信固定ip宽带80与8080端口踩坑

    本文只是作为记录,避免后面遇到此类问题耗费时间. 实际情况:公司有个固定电信宽带是固定IP的,想把固定IP映射到测试环境ip,实现可以公网通过固定ip访问,内网通过局域网ip访问. 测试环境服务是占用 ...

  9. XML CData 处理

    调研了 JAXB.XMLMapper(jackson) 具体方式 实现 优势 JAXB 1. 需要增加 CDATA 的Adaptor 2. 需要增加对非CDATA 的 CharacterEscapeH ...

  10. C#正则表达式合并连续空格为单个空格

    第一种方法: 使用 System.Text.RegularExpressions.Regex.Replace()方法 string result = String.Empty; string str ...