LeetCode - Construct String from Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: string tree2str(TreeNode* t) { if(!t) return ""; string left = tree2str(t->left); string right = tree2str(t->right); if(left==""&&right=="") return to_string(t->val); if(right==""){ return to_string(t->val)+"("+left+")"; }else{ return to_string(t->val)+"("+left+")"+"("+right+")"; } } };
相關推薦
LeetCode - Construct String from Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :
LeetCode Construct String from Binary Tree 根據二叉樹建立字串
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be
[LeetCode] Construct String from Binary Tree 根據二叉樹建立字串
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be repre
[Leetcode] Binary tree-- 606. Construct String from Binary Tree
between bstr exce out sts elif ide hole enc You need to construct a string consists of parenthesis and integers from a binary tree with t
[LeetCode&Python] Problem 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represe
LeetCode:606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be r
leetcode (Construct String from Binary Tree)
Title:Construct String from Binary Tree 606 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/construct-string-from-b
LeetCode-606. Construct String from Binary Tree (Java)
題目連結 此題是關於二叉樹的,先回顧一下關於二叉樹的相關知識 二叉樹的先序遍歷(根節點,左孩子,右孩子): public class PreTreeSolution { public static String preorderTraversal(TreeNod
Leetcode PHP題解--D92 606. Construct String from Binary Tree
D92 606. Construct String from Binary Tree 題目連結 606. Construct St
606. Construct String from Binary Tree
type break elf nsh tps self. question ssa -o https://leetcode.com/problems/construct-string-from-binary-tree/#/description You need t
606. Construct String from Binary Tree 【easy】
new mos pen you image color emp same exce 606. Construct String from Binary Tree 【easy】 You need to construct a string consists of paren
Leetcode606.Construct String from Binary Tree根據二叉樹建立字串
你需要採用前序遍歷的方式,將一個二叉樹轉換成一個由括號和整陣列成的字串。 空節點則用一對空括號 "()" 表示。而且你需要省略所有不影響字串與原始二叉樹之間的一對一對映關係的空括號對。 示例 1: 輸入: 二叉樹: [1,2,3,4]
Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be repre
606. Construct String from Binary Tree(python+cpp)
題目: You need to construct a string consists of parenthesis and integers from a binary tree with the
606. Construct String from Binary Tree - Easy
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represe
[Algorithm] Construct String from Binary Tree
empty exc pla ces color pty ddr put ole You need to construct a string consists of parenthesis and integers from a binary tree with the p
[leetcode-543-Diameter of Binary Tree]
iam ret class 二叉樹 topic eight tco diameter amp Given a binary tree, you need to compute the length of the diameter of the tree. The dia
LeetCode 543. Diameter of Binary Tree (二叉樹的直徑)
tween res edge public level 距離 sent java dot Given a binary tree, you need to compute the length of the diameter of the tree. The diamet
[LeetCode] Maximum Width of Binary Tree 二叉樹的最大寬度
example imu idt count ted pla con integer out Given a binary tree, write a function to get the maximum width of the given tree. The wi
Leetcode 543: Diameter of Binary Tree
des them null fin private log ive pass leet Given a binary tree, you need to compute the length of the diameter of the tree. The diameter