tree

226. Invert Binary Tree

题目链接:https://leetcode.com/problems/invert-binary-tree/#/description

题目大意:反转一棵二叉树

思路:递归反转左子树和右子树,然后再交换左子树和右子树的根节点即可。

算法复杂度分析:时间复杂度O(n),空间复杂度O(log(n))

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* invertTree(TreeNode* root) {
13         if (!root || (!root->left && !root->right))
14             return root;
15         invertTree(root->left);
16         invertTree(root->right);
17         swap(root->left, root->right);
18         return root;
19     }
20 };

评测系统上运行结果:

非递归版本代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* invertTree(TreeNode* root) {
13         if (!root)
14             return root;
15         queue<TreeNode*> nodes;
16         nodes.push(root);
17         while (!nodes.empty()) {
18             TreeNode *tn = nodes.front();
19             nodes.pop();
20             swap(tn->left, tn->right);
21             if (tn->left)
22                 nodes.push(tn->left);
23             if (tn->right)
24                 nodes.push(tn->right);
25         }
26         return root;
27     }
28 };

144. Binary Tree Preorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-preorder-traversal/#/description

题目大意:前序遍历二叉树(非递归)

思路:用一个栈来模拟递归的过程

算法复杂度分析:时间复杂度O(n),空间复杂度O(log(n))

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode* root) {
13         vector<int> res;
14         if (!root)
15             return res;
16         stack<TreeNode*> nodes;
17         nodes.push(root);
18         res.push_back(root->val);
19         while (!nodes.empty()) {
20             auto tn = nodes.top();
21             if (tn->left) {
22                 nodes.push(tn->left);
23                 res.push_back(tn->left->val);
24                 tn->left = nullptr;
25             } else {
26                 nodes.pop();
27                 if (tn->right) {
28                     nodes.push(tn->right);
29                     res.push_back(tn->right->val);
30                 }
31             }
32         }
33         return res;
34     }
35 };

评测系统上运行结果:

94. Binary Tree Inorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/#/description

题目大意:中序遍历二叉树(非递归)

思路:用一个栈来模拟递归的过程

算法复杂度分析:时间复杂度O(n),空间复杂度O(log(n))

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> inorderTraversal(TreeNode* root) {
13         vector<int> res;
14         if (!root)
15             return res;
16         stack<TreeNode*> nodestack;
17         nodestack.push(root);
18         while (!nodestack.empty()) {
19             TreeNode *tn = nodestack.top();
20             if (tn->left) {
21                 nodestack.push(tn->left);
22                 tn->left = nullptr;
23             }
24             else {
25                 res.push_back(tn->val);
26                 nodestack.pop();
27                 if (tn->right)
28                     nodestack.push(tn->right);
29             }
30         }
31         return res;
32     }
33 };

 评测系统上运行结果:

145. Binary Tree Postorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/#/description

题目大意:后序遍历二叉树(非递归)

思路:用一个栈来模拟递归的过程

算法复杂度分析:时间复杂度O(n),空间复杂度O(log(n))

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> postorderTraversal(TreeNode* root) {
13         vector<int> res;
14         if (!root)
15             return res;
16         stack<TreeNode*> nodes;
17         nodes.push(root);
18         while (!nodes.empty()) {
19             auto tn = nodes.top();
20             if (tn->left) {
21                 nodes.push(tn->left);
22                 tn->left = nullptr;
23             } else if (tn->right) {
24                 nodes.push(tn->right);
25                 tn->right = nullptr;    
26             } else {
27                 nodes.pop();
28                 res.push_back(tn->val);
29             }
30         }
31         return res;
32     }
33 };

 评测系统上运行结果:

102. Binary Tree Level Order Traversal

题目链接:https://leetcode.com/problems/binary-tree-level-order-traversal/#/description

题目大意:水平遍历二叉树(非递归)

思路:用一个队列来保存每个层级的所有节点,同时用一个整型num来记录每个level的节点数

算法复杂度分析:时间复杂度O(n),空间复杂度O(n)

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> levelOrder(TreeNode* root) {
13         vector<vector<int>> res;
14         if (!root)
15             return res;
16         queue<TreeNode*> nodes;
17         nodes.push(root);
18         int nodeNum = 1;
19         while (!nodes.empty()) {
20             vector<int> vec;
21             int count = 0;
22             while (nodeNum--) {
23                 auto tn = nodes.front();
24                 nodes.pop();
25                 vec.push_back(tn->val);
26                 if (tn->left) {
27                     nodes.push(tn->left);
28                     ++count;
29                 }
30                 if (tn->right) {
31                     nodes.push(tn->right);
32                     ++count;
33                 }
34             }
35             nodeNum = count;
36             res.push_back(vec);
37         }
38         return res;
39     }
40 };

 评测系统上运行结果:

原文地址:https://www.cnblogs.com/gxhblog/p/6597035.html