[LeetCode]606. Construct String from Binary Tree
来源:互联网 发布:手机怎么看本机mac地址 编辑:程序博客网 时间:2024/05/16 05:47
[LeetCode]606. Construct String from Binary Tree
题目描述
思路
二叉树的先序遍历扩展,注意处理子节点为空的三种情况
代码
#include <iostream>#include <string>using namespace std;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 == NULL) return ""; if (t->left == NULL && t->right == NULL) return to_string(t->val) + ""; if (t->right == NULL) return to_string(t->val) + "(" + tree2str(t->left) + ")"; return to_string(t->val) + "(" + tree2str(t->left) + ")" + "(" + tree2str(t->right) + ")"; }};int main() { TreeNode* t1 = new TreeNode(1); TreeNode* t2 = new TreeNode(2); TreeNode* t3 = new TreeNode(3); TreeNode* t4 = new TreeNode(4); t1->left = t2; t1->right = t3; t2->left = t4; Solution s; cout << s.tree2str(t1) << endl; system("pause");}
阅读全文
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)
- HDFS概述
- idea 中Java web项目只中创建Java main方法运行出现ClassNotFoundException
- Shell脚本简单练习
- Spark 性能优化总结(思维导图)
- SQL Server2008 远程过程调用失败 0x800706be
- [LeetCode]606. Construct String from Binary Tree
- MySQL 主从架构之读写分离
- 一个.exe文件通过注册表获取另一个.exe最新的版本来打开应用程序
- IScroll的那些事——内容不足时下拉刷新
- docker命令
- Spark 广播 (思维导图)
- webpack3最新版本配置研究(四)webpack压缩,clean-webpack-plugin,webpack-manifest-plugin,define-plugin
- ios "_OBJC_CLASS_$_xxx", referenced from: 错误
- 170717 逆向-Smali函数分析