leetcode-606. Construct String from Binary Tree
来源:互联网 发布:win7其他 软件卸载 编辑:程序博客网 时间:2024/06/05 05:37
考察点:递归,二叉树;
思路:注意初始情况就行。
C++代码:
/** * 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 help(TreeNode* t) { if (t->left==NULL && t->right == NULL) return "(" + to_string(t->val) + ")"; if (t->left==NULL && t->right) { return "("+to_string(t->val)+"()" + help(t->right) + ")"; } if (t->left && t->right==NULL) { return "("+to_string(t->val)+help(t->left) + ")"; } if (t->left && t->right) { return "("+to_string(t->val)+help(t->left) + help(t->right)+")"; } } string tree2str(TreeNode* t) { if (t == NULL) return ""; if (t->left==NULL && t->right == NULL) return to_string(t->val); if (t->left==NULL && t->right) { return to_string(t->val)+"()" + help(t->right); } if (t->left && t->right==NULL) { return to_string(t->val)+help(t->left); } if (t->left && t->right) { return to_string(t->val)+help(t->left) + help(t->right); } }};
阅读全文
0 0
- [leetcode]606. Construct String from Binary Tree
- [leetcode]: 606. Construct String from Binary Tree
- [LeetCode] 606. Construct String from Binary Tree
- Leetcode 606. Construct String from Binary Tree
- LeetCode: 606. Construct String from Binary Tree
- [LeetCode]606. Construct String from Binary Tree
- leetcode 606. Construct String from Binary Tree
- LeetCode 606. Construct String from Binary Tree
- leetcode: 606. Construct String from Binary Tree
- Leetcode-606. Construct String from Binary Tree
- leetcode-606. Construct String from Binary Tree
- leetcode-606. Construct String from Binary Tree
- Leetcode-536. Construct Binary Tree from String
- LeetCode 606 Construct String from Binary Tree
- LeetCode 606 Construct String from Binary Tree
- LeetCode Construct String from Binary Tree
- leetcode 606 Construct String from Binary Tree
- Construct String from Binary Tree(leetcode)
- 一篇实用的Latex的入门教程
- 国庆清北刷题冲刺班 Day7 下午
- Leetcode||34. Search for a Range
- 十大管理思维导图
- Ant介绍及标签用法
- leetcode-606. Construct String from Binary Tree
- webrtc 1M码流限制
- jquery.ajax()向后台传递数组数据问题,java后台无法接受
- C#标识符关键字
- Linux ALSA声卡驱动之二:声卡的创建
- 第四周项目 项目2
- Linux中zip压缩和unzip解压缩命令详解
- 高性能、高并发、高扩展性和可读性的网络服务器架构:StateThreads
- java的基本语言元素--关键字