日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.
  • An empty tree is represented by?""?instead of?"()".
  • ?[暴力解法]:

    時間分析:

    空間分析:

    ?[優化后]:

    時間分析:

    空間分析:

    [奇葩輸出條件]:

    [奇葩corner case]:

    注意下:取數可以多取幾位,i+1位是數字時就繼續i++

    [思維問題]:

    感覺我在背題:幾天不背,功力全無。substring都忘了。

    [英文數據結構或算法,為什么不用別的數據結構或算法]:

    [一句話思路]:

    [輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):

    [畫圖]:

    [一刷]:

    ?

  • new TreeNode(Integer.valueOf(s.substring(j, i + 1)))字符串不能直接轉node,需要轉interger后再轉node
  • 一直往后移用的是while循環
  • [二刷]:

  • 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 caseif (s == null || s.length() == 0) return null;//initialization: stackStack<TreeNode> stack = new Stack<TreeNode>();//for loop: new node, get substring and appendfor (int i = 0, j = i; i < s.length(); i++, j = i) {//get cchar c = s.charAt(i);//if c is )if (c == ')') stack.pop();else if ((c >= '0' && c <= '9') || (c == '-')) {//continuewhile (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') i++;//build new nodeTreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));if (!stack.isEmpty()) {TreeNode parent = stack.peek();//get left and appendif (parent.left != null) {parent.right = node;}else parent.left = node;}stack.push(node);}}//return the last rootreturn stack.peek() == null ? null : stack.pop();} } View Code

    ?

    轉載于:https://www.cnblogs.com/immiao0319/p/9612471.html

    總結

    以上是生活随笔為你收集整理的536. Construct Binary Tree from String 从括号字符串中构建二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。