[LintCode] 二叉树的前序遍历

本文介绍两种二叉树前序遍历的方法:一种使用栈实现,时间复杂度和空间复杂度均为O(n);另一种采用Morris遍历算法,时间复杂度为O(n),但仅需O(1)的空间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The recursive solution is trivial and I omit it here.

Iterative Solution using Stack (O(n) time and O(n) space):

 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 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> nodes;
23         TreeNode* node = root;
24         stack<TreeNode*> right;
25         while (node || !right.empty()) {
26             if (node) {
27                 nodes.push_back(node -> val);
28                 if (node -> right)
29                     right.push(node -> right);
30                 node = node -> left;
31             }
32             else {
33                 node = right.top();
34                 right.pop();
35             }
36         }
37         return nodes;
38     }
39 };

Another more sophisticated solution using Morris Traversal (O(n) time and O(1) space):

 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 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> nodes;
23         TreeNode* node = root;
24         while (node) {
25             if (node -> left) {
26                 TreeNode* predecessor = node -> left;
27                 while (predecessor -> right && predecessor -> right != node)
28                     predecessor = predecessor -> right;
29                 if (!(predecessor -> right)) {
30                     nodes.push_back(node -> val);
31                     predecessor -> right = node;
32                     node = node -> left;
33                 }
34                 else {
35                     predecessor -> right = NULL;
36                     node = node -> right;
37                 }
38             }
39             else {
40                 nodes.push_back(node -> val);
41                 node = node -> right;
42             }
43         }
44         return nodes;
45     }
46 };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值