博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LintCode: Binary Tree Postorder Traversal
阅读量:6683 次
发布时间:2019-06-25

本文共 3764 字,大约阅读时间需要 12 分钟。

C++,递归

1 /** 2  * Definition of TreeNode: 3  * class TreeNode { 4  * public: 5  *     int val; 6  *     TreeNode *left, *right; 7  *     TreeNode(int val) { 8  *         this->val = val; 9  *         this->left = this->right = NULL;10  *     }11  * }12  */13 class Solution {14     /**15      * @param root: The root of binary tree.16      * @return: Postorder in vector which contains node values.17      */18 public:19     vector
postorderTraversal(TreeNode *root) {20 // write your code here21 vector
result;22 if (root == NULL) {23 return result;24 }25 if (root->left != NULL) {26 vector
left = postorderTraversal(root->left);27 result.reserve(result.size() + left.size());28 result.insert(result.end(), left.begin(), left.end());29 }30 if (root->right != NULL) {31 vector
right = postorderTraversal(root->right);32 result.reserve(result.size() + right.size());33 result.insert(result.end(), right.begin(), right.end());34 }35 result.push_back(root->val);36 return result;37 }38 };

C++,递归,辅助函数

1 /** 2  * Definition of TreeNode: 3  * class TreeNode { 4  * public: 5  *     int val; 6  *     TreeNode *left, *right; 7  *     TreeNode(int val) { 8  *         this->val = val; 9  *         this->left = this->right = NULL;10  *     }11  * }12  */13 class Solution {14     /**15      * @param root: The root of binary tree.16      * @return: Postorder in vector which contains node values.17      */18 public:19     vector
postorderTraversal(TreeNode *root) {20 // write your code here21 vector
result;22 if (root == NULL) {23 return result;24 } else {25 postorderCore(root, result);26 }27 return result;28 }29 void postorderCore(TreeNode *root, vector
&result) {30 if (root == NULL) {31 return;32 }33 if (root->left != NULL) {34 postorderCore(root->left, result);35 }36 if (root->right != NULL) {37 postorderCore(root->right, result);38 }39 result.push_back(root->val);40 return;41 } 42 };

C++,非递归

[一个stack]

[一个cur指针]

[一个pre指针]

1 /** 2  * Definition of TreeNode: 3  * class TreeNode { 4  * public: 5  *     int val; 6  *     TreeNode *left, *right; 7  *     TreeNode(int val) { 8  *         this->val = val; 9  *         this->left = this->right = NULL;10  *     }11  * }12  */13 class Solution {14     /**15      * @param root: The root of binary tree.16      * @return: Postorder in vector which contains node values.17      */18 public:19     vector
postorderTraversal(TreeNode *root) {20 // write your code here21 vector
result;22 if (root == NULL) {23 return result;24 }25 26 TreeNode *cur = root, *pre = NULL;27 stack
sta;28 29 while (cur != NULL || !sta.empty()) {30 while (cur != NULL) {31 sta.push(cur);32 cur = cur->left;33 }34 cur = sta.top();35 if (cur->right == NULL || cur->right == pre) {36 sta.pop();37 result.push_back(cur->val);38 pre = cur;39 cur = NULL;40 } else {41 cur = cur->right;42 }43 }44 return result;45 }46 };

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4999458.html,如需转载请自行联系原作者

你可能感兴趣的文章
独家揭秘语音视频聊天室开发顶尖制作教程
查看>>
冲向大牛之安卓:学习界面怎么在程序中画出来
查看>>
.net 签名加密实现的一种简单方法
查看>>
数据结构 试探法算法学习笔记
查看>>
nomad安装
查看>>
我的友情链接
查看>>
MySQL主备复制数据不一致的情况
查看>>
CU3ER非常Cool的3D效果的Flash Slider
查看>>
中财讯 爆遍历目录漏洞
查看>>
MongoDB 数据库备份脚本
查看>>
Linux常用命令
查看>>
10、《每天5分钟玩转Docker容器技术》学习-Docker命令之本地镜像管理
查看>>
shell脚本:输出昨天的日期
查看>>
corosync+pacemaker做高可用web集群
查看>>
mysql中各个模块如何协同工作
查看>>
MyEclipse - 在tomcat6里面配置tomcat7
查看>>
less新手入门(五)—— CssGuards、循环、合并
查看>>
我的友情链接
查看>>
当sd卡不存在时,保存文件到手机上
查看>>
android动画资料汇总
查看>>