|
54 | 54 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 55 |
|
56 | 56 | ```python
|
57 |
| - |
| 57 | +# Definition for a binary tree node. |
| 58 | +# class TreeNode: |
| 59 | +# def __init__(self, val=0, left=None, right=None): |
| 60 | +# self.val = val |
| 61 | +# self.left = left |
| 62 | +# self.right = right |
| 63 | +class Solution: |
| 64 | + def tree2str(self, root: Optional[TreeNode]) -> str: |
| 65 | + def dfs(root): |
| 66 | + if root is None: |
| 67 | + return '' |
| 68 | + if root.left is None and root.right is None: |
| 69 | + return str(root.val) |
| 70 | + if root.right is None: |
| 71 | + return f'{root.val}({dfs(root.left)})' |
| 72 | + return f'{root.val}({dfs(root.left)})({dfs(root.right)})' |
| 73 | + |
| 74 | + return dfs(root) |
58 | 75 | ```
|
59 | 76 |
|
60 | 77 | ### **Java**
|
61 | 78 |
|
62 | 79 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 80 |
|
64 | 81 | ```java
|
| 82 | +/** |
| 83 | + * Definition for a binary tree node. |
| 84 | + * public class TreeNode { |
| 85 | + * int val; |
| 86 | + * TreeNode left; |
| 87 | + * TreeNode right; |
| 88 | + * TreeNode() {} |
| 89 | + * TreeNode(int val) { this.val = val; } |
| 90 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 91 | + * this.val = val; |
| 92 | + * this.left = left; |
| 93 | + * this.right = right; |
| 94 | + * } |
| 95 | + * } |
| 96 | + */ |
| 97 | +class Solution { |
| 98 | + public String tree2str(TreeNode root) { |
| 99 | + if (root == null) { |
| 100 | + return ""; |
| 101 | + } |
| 102 | + if (root.left == null && root.right == null) { |
| 103 | + return root.val + ""; |
| 104 | + } |
| 105 | + if (root.right == null) { |
| 106 | + return root.val + "(" + tree2str(root.left) + ")"; |
| 107 | + } |
| 108 | + return root.val + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")"; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### **C++** |
| 114 | + |
| 115 | +```cpp |
| 116 | +/** |
| 117 | + * Definition for a binary tree node. |
| 118 | + * struct TreeNode { |
| 119 | + * int val; |
| 120 | + * TreeNode *left; |
| 121 | + * TreeNode *right; |
| 122 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 123 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 124 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 125 | + * }; |
| 126 | + */ |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + string tree2str(TreeNode* root) { |
| 130 | + if (!root) return ""; |
| 131 | + if (!root->left && !root->right) return to_string(root->val); |
| 132 | + if (!root->right) return to_string(root->val) + "(" + tree2str(root->left) + ")"; |
| 133 | + return to_string(root->val) + "(" + tree2str(root->left) + ")(" + tree2str(root->right) + ")"; |
| 134 | + } |
| 135 | +}; |
| 136 | +``` |
65 | 137 |
|
| 138 | +### **Go** |
| 139 | +
|
| 140 | +```go |
| 141 | +/** |
| 142 | + * Definition for a binary tree node. |
| 143 | + * type TreeNode struct { |
| 144 | + * Val int |
| 145 | + * Left *TreeNode |
| 146 | + * Right *TreeNode |
| 147 | + * } |
| 148 | + */ |
| 149 | +func tree2str(root *TreeNode) string { |
| 150 | + if root == nil { |
| 151 | + return "" |
| 152 | + } |
| 153 | + if root.Left == nil && root.Right == nil { |
| 154 | + return strconv.Itoa(root.Val) |
| 155 | + } |
| 156 | + if root.Right == nil { |
| 157 | + return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")" |
| 158 | + } |
| 159 | + return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")(" + tree2str(root.Right) + ")" |
| 160 | +} |
66 | 161 | ```
|
67 | 162 |
|
68 | 163 | ### **...**
|
|
0 commit comments