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. JavaScript 函数定义方法

    JavaScript 函数定义方法. 函数声明 在之前的教程中,你已经了解了函数声明的语法 : function functionName(parameters) { 执行的代码 } 函数声明后不会立 ...

  2. Linux CentOS7系统中php安装配置

    本篇讲解如何配置php开发环境,让你的php代码可以正常的在网页中运行. 准备工作 linux centos7操作系统 ssh软件 nginx php资源 想要了解更多关于php的内容,请访问: ph ...

  3. Linux CentOS7系统中ssh的用法

    大家都知道,公司买上服务器,不可能实时在线操作虚拟机,也没有那个时间和精力登录到公司的云服务商官网进行操作,一来不安全,二来也效率不高. 如果是购买的虚拟主机,你可以使用ftp进行本地程序文件传输和从 ...

  4. Codeforces Round #556 (Div. 2)

    比赛链接 A 贪心 #include <cstdlib> #include <cstdio> #include <algorithm> #include <c ...

  5. document文档对象

    document 文挡对象 - JavaScript脚本语言描述———————————————————————注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写否则会提示你一 ...

  6. (转)pt-online-schema-change在线修改表结构

    原文:http://www.ywnds.com/?p=4442 一.背景 MySQL大字段的DDL操作:加减字段.索引.修改字段属性等,在5.1之前都是非常耗时耗力的,特别是会对MySQL服务产生影响 ...

  7. C# 对象相等性判断和同一性判断

    在日常开发中经常需要编写代码比较不同的对象.例如,有时需要将对象都放到一个集合中,并编写代码对集合中的对象进行排序.搜索或者比较. System.Object类有两个Equals方法,如下: 1.实例 ...

  8. Spring Security构建Rest服务-1000-使用SpringSocial开发第三方登录之大白话OAuth协议

    OAuth协议简介 OAuth协议要解决的问题    OAuth协议中的各种角色 OAuth协议运行流程 OAuth协议,在网上也看了一些资料,意思就是给你颁发一个临时的通行证,你拿着这个通行证可以访 ...

  9. 弹幕和回到顶部前端web

    弹幕和回到顶部前端web 弹幕 1.效果演示 2.相关代码 <!DOCTYPE html> <html lang="en"> <head> &l ...

  10. hbase copyTable

    参考:https://yq.aliyun.com/articles/176546 执行:hbase org.apache.hadoop.hbase.mapreduce.CopyTable --new. ...