LeetCode 536----Construct Binary Tree from String
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:
- 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的更多相关文章
- [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...
- 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...
- 536. Construct Binary Tree from String 从括号字符串中构建二叉树
[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...
- (二叉树 递归) 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 ...
- (二叉树 递归) 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 ...
- [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 ...
- [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 ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [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 ...
随机推荐
- day 31 html(二) 和css入门
前情提要: 本次主要是继续昨天学的简单的html 补充以及 css的简单入门 一:表单标签 >1:get请求 <!DOCTYPE html> <html lang=" ...
- cat /proc/sys/net/ipv4/ip_forward 0 解决办法
[root@localhost java]# cat /proc/sys/net/ipv4/ip_forward 出于安全考虑,Linux系统默认是禁止数据包转发的. 所谓转发即当主机拥有多于一块的网 ...
- Java模拟双色球彩票
package practice1; import java.util.Random; import java.util.Scanner; public class Test3 { /** * * 模 ...
- windows下python3.6版本安装pygame
参考:http://blog.csdn.net/a380331382/article/details/77063152 首先,进入这个网站:http://www.lfd.uci.edu/~gohlke ...
- 慕课网Python基础学习整理
# -*- coding: utf-8 -*- """# Python的注释以 # 开头,后面的文字直到行尾都算注释;多行注释开头3个 " 结尾3个 " ...
- C/C++ -- Gui编程 -- Qt库的使用 -- 标准对话框
-----mywidget.cpp----- #include "mywidget.h" #include "ui_mywidget.h" #include & ...
- java 算法 - 冒泡排序
冒泡排序: 冒泡排序是专门针对已有的一部分已经排序的数据进行排序的一种排序算法.假如你的数据中只有两个数据输乱序的,那么冒泡排序就是最快的.这种算法的核心思想就是扫描数据清单,找到乱序中相邻的两个数据 ...
- git工具,conflict冲突解决方法
这篇文章需要对git具有一定的了解,并且知道如何安装git工具(其实就是上git官网下载个文件,安装) git这种版本控制工具有什么好处 第一个,方便可以多人协同开发同一个项目或系统 第二个,当你系统 ...
- tomcat启动(四)Catalina分析-server的init()方法
上一回load()方法解析讲到xml解析完成. load()内部接下来会获取server getServer().setCatalina(this); 这个server从createStartDige ...
- centos 关闭selinux 临时关闭selinux 报错 setenforce: setenforce() failed
关闭selinux的方法有两种:临时关闭和永久关闭. 查看selinux的状态:estatus [root@--- ~]# sestatus SELinux status: enabled SELin ...